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

发布时间:2020-08-22 05:22:15 作者:松鼠大帝
来源:脚本之家 阅读:202

用途

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

推荐阅读:
  1. Python中logging模块的操作
  2. Python系统模块sys的功能有哪些?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python logging 模块

上一篇:pyhton列表转换为数组的实例

下一篇:VueJs 搭建Axios接口请求工具

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》