python logging system
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python logging system,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3691字,纯文字阅读大概需要6分钟。
内容图文
官方教程:https://docs.python.org/2/library/logging.html
1. 用法1
import logging import logging.handlers LOG_FILE = ‘tst.log‘ handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler fmt = ‘%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s‘ formatter = logging.Formatter(fmt) # 实例化formatter handler.setFormatter(formatter) # 为handler添加formatter logger = logging.getLogger(‘tst‘) # 获取名为tst的logger logger.addHandler(handler) # 为logger添加handler logger.setLevel(logging.DEBUG) logger.info(‘first info message‘) logger.debug(‘first debug message‘)
formatter
采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:
Format | Description |
---|---|
%(name)s | Name of the logger (logging channel). |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
%(levelname)s | Text logging level for the message (‘DEBUG‘, ‘INFO‘, ‘WARNING‘, ‘ERROR‘, ‘CRITICAL‘). |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(filename)s | Filename portion of pathname. |
%(module)s | Module (name portion of filename). |
%(funcName)s | Name of function containing the logging call. |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(created)f | Time when the LogRecord was created (as returned by time.time()). |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
%(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time). |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. |
%(thread)d | Thread ID (if available). |
%(threadName)s | Thread name (if available). |
%(process)d | Process ID (if available). |
%(message)s | The logged message, computed as msg % args. |
level
The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost.
Level | Numeric value |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
如果指定level 为INFO, 则小于它的所有message都不会打印出来。
2. 用法2
采用配置文件的方式去配置logging system, 这种方法简单方便,可以在多模块间调用而不需要传入logger对象。
现在我准备一个配置文件logging.conf如下
[loggers] keys=root,uploader,msgChanger # [handlers] keys=consoleHandler,uploaderHandler,msgChangerHandler [formatters] keys=fmt [logger_root] level=INFO handlers=consoleHandler qualname=root [logger_uploader] level=INFO qualname=uploader handlers=uploaderHandler [logger_msgChanger] level=INFO qualname=msgChanger handlers=msgChangerHandler [handler_consoleHandler] class=StreamHandler level=INFO formatter=fmt args=(sys.stdout,) [handler_uploaderHandler] class=logging.handlers.RotatingFileHandler level=INFO formatter=fmt args=(‘log/uploader.log‘,‘w‘,1024*1024*1024,5,) [handler_msgChangerHandler] class=logging.handlers.RotatingFileHandler level=INFO formatter=fmt args=(‘log/msg_changer.log‘,‘w‘,1024*1024*1024,5,) [formatter_fmt] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s datefmt=
called in code:
‘‘‘init log module‘‘‘ logging.config.fileConfig(‘config/logging.conf‘) # parse config file as above msgChanger_logger = logging.getLogger(‘msgChanger.main‘) # get a logger instance, msgChanger must be included
msgChanger_logger.info(‘test log system in main.‘)
可以在任何module下写入上述代码调用logger, 只需要传入一个名字就好了,名字必须在config文件里配置了的,否则实例失败。
原文:http://www.cnblogs.com/lovemo1314/p/3812572.html
内容总结
以上是互联网集市为您收集整理的python logging system全部内容,希望文章能够帮你解决python logging system所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。