Python常用模块logging――日志输出功能(示例代码)

logging模块是Python的内置模块,主要用于输出运行日志,可以灵活配置输出日志的各项信息。这篇文章主要介绍了Python常用模块logging――日志输出的实例代码,需要的朋友可以参考下

用途

logging模块是Python的内置模块,主要用于输出运行日志,可以灵活配置输出日志的各项信息。

基本使用方法

 logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a")

参数

日志一共分成5个等级,从低到高分别是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。

%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息

调用

 logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message')

示例

 import logging logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a") logging.debug("Hello")

日志文件loggmsg.log

levelname:DEBUG filename: test.py outputNumber: [7]  thread: MainThread output msg:  Hello -

总结

以上所述是小编给大家介绍的Python常用模块logging――日志输出功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对html中文网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

以上就是Python常用模块logging――日志输出功能(示例代码)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python