Python logging模块

基本用法

# -*- coding: utf-8 -*-

import logging
import sys

# 获取logger实例,如果参数为空则返回root logger
logger = logging.getLogger("AppName")

# 指定logger输出格式
formatter = logging.Formatter('[%(asctime)s] %(levelname)-8s: %(message)s')

# 文件日志
file_handler = logging.FileHandler("test.log")
file_handler.setFormatter(formatter)  # 可以通过setFormatter指定输出格式

# 控制台日志
console_handler = logging.StreamHandler(sys.stdout)
console_handler.formatter = formatter  # 也可以直接给formatter赋值

# 为logger添加的日志处理器
logger.addHandler(file_handler)
logger.addHandler(console_handler)

# 指定日志的最低输出级别,默认为WARN级别
logger.setLevel(logging.INFO)

# 输出不同级别的log
logger.debug('this is debug info')
logger.info('this is information')
logger.warn('this is warning message')
logger.error('this is error message')
logger.fatal('this is fatal message, it is same as logger.critical')
logger.critical('this is critical message')

# [2018-05-17 23:51:10,486] INFO    : this is information
# [2018-05-17 23:51:10,486] WARNING : this is warning message
# [2018-05-17 23:51:10,486] ERROR   : this is error message
# [2018-05-17 23:51:10,486] CRITICAL: this is fatal message, it is same as logger.critical
# [2018-05-17 23:51:10,486] CRITICAL: this is critical message
# [Finished in 0.3s]

# 移除一些日志处理器
# logger.removeHandler(file_handler)

# 日志等级,DEBUG最低,CRITICAL最高
logger.info(logging.DEBUG)
logger.info(logging.INFO)
logger.info(logging.WARN)
logger.info(logging.ERROR)
logger.info(logging.CRITICAL)

输出:

[2018-05-18 00:08:17,591] INFO    : this is information
[2018-05-18 00:08:17,591] WARNING : this is warning message
[2018-05-18 00:08:17,591] ERROR   : this is error message
[2018-05-18 00:08:17,591] CRITICAL: this is fatal message, it is same as logger.critical
[2018-05-18 00:08:17,591] CRITICAL: this is critical message
[2018-05-18 00:08:17,591] INFO    : 10
[2018-05-18 00:08:17,592] INFO    : 20
[2018-05-18 00:08:17,592] INFO    : 30
[2018-05-18 00:08:17,592] INFO    : 40
[2018-05-18 00:08:17,592] INFO    : 50
[Finished in 0.2s]

LogRecord attributes

Attribute nameFormatDescription
argsYou shouldn’t need to format this yourself.The tuple of arguments merged into msg to produce message, or a dict whose values are used for the merge (when there is only one argument, and it is a dictionary).
asctime%(asctime)sHuman-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).
created%(created)fTime when the LogRecord was created (as returned by time.time()).
exc_infoYou shouldn’t need to format this yourself.Exception tuple (à la sys.exc_info) or, if no exception has occurred, None.
filename%(filename)sFilename portion of pathname.
funcName%(funcName)sName of function containing the logging call.
levelname%(levelname)sText logging level for the message (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’).
levelno%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
lineno%(lineno)dSource line number where the logging call was issued (if available).
message%(message)sThe logged message, computed as msg % args. This is set when Formatter.format() is invoked.
module%(module)sModule (name portion of filename).
msecs%(msecs)dMillisecond portion of the time when the LogRecord was created.
msgYou shouldn’t need to format this yourself.The format string passed in the original logging call. Merged with args to produce message, or an arbitrary object (see Using arbitrary objects as messages).
name%(name)sName of the logger used to log the call.
pathname%(pathname)sFull pathname of the source file where the logging call was issued (if available).
process%(process)dProcess ID (if available).
processName%(processName)sProcess name (if available).
relativeCreated%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
stack_infoYou shouldn’t need to format this yourself.Stack frame information (where available) from the bottom of the stack in the current thread, up to and including the stack frame of the logging call which resulted in the creation of this record.
thread%(thread)dThread ID (if available).
threadName%(threadName)sThread name (if available).

参考资料:
https://docs.python.org/3/library/logging.html
http://python.jobbole.com/86887/
https://blog.csdn.net/huilan_same/article/details/51858817

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值