首页 > 开发 > Python > 正文

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

2020-07-28 14:08:03
字体:
来源:转载
供稿:网友

用途

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 logginglogging.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――日志输出功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对错新网之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表