logging模块的使用

tutorial(YouTube视频):https://www.youtube.com/watch?v=g8nQ90Hk328&t=5s

更detail:Python之日志处理(logging模块) https://www.cnblogs.com/yyds/p/6901864.html

转载于:https://www.cnblogs.com/LLgloria/p/10061947.html

logging模块Python中用于记录日志的标准库。下面是一个使用logging模块的基本场景使用实例\[1\]: ```python import logging # 配置日志输出的格式 logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) # 创建一个logger对象 logger = logging.getLogger(__name__) # 输出不同级别的日志 logger.debug('This is a debug message') logger.info('This is an info message') logger.warning('This is a warning message') logger.error('This is an error message') logger.critical('This is a critical message') ``` 在上面的例子中,我们首先通过`basicConfig`函数配置了日志输出的格式和级别。然后,我们创建了一个名为`__name__`的logger对象,这样做的好处是可以在日志中明确显示消息来自哪个模块\[2\]。接下来,我们使用不同级别的方法来输出日志信息。 除了上述基本用法,还可以通过配置文件或使用`logging.config`模块来配置日志\[3\]。通过配置文件可以更灵活地配置不同模块的日志输出。在实际开发中,可以先编写好一个配置文件,然后在主函数中通过`fileConfig`加载配置文件。然后,在不同的模块中可以使用根logger的子logger来进行日志记录,而不需要反复定义和配置各个模块的logger。 希望以上内容对您有帮助。如果需要更详细和灵活的使用方法,请参考官方帮助文档:https://docs.python.org/zh-cn/3/howto/logging.html。 #### 引用[.reference_title] - *1* [[ Python入门教程 ] Python中日志记录模块logging使用实例](https://blog.youkuaiyun.com/weixin_32687283/article/details/113652063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [logging模块详细使用](https://blog.youkuaiyun.com/lucylily11/article/details/116050564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值