基本用法
# -*- 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 name | Format | Description |
---|---|---|
args | You 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)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). |
created | %(created)f | Time when the LogRecord was created (as returned by time.time()). |
exc_info | You shouldn’t need to format this yourself. | Exception tuple (à la sys.exc_info) or, if no exception has occurred, None. |
filename | %(filename)s | Filename portion of pathname. |
funcName | %(funcName)s | Name of function containing the logging call. |
levelname | %(levelname)s | Text logging level for the message (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’). |
levelno | %(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
lineno | %(lineno)d | Source line number where the logging call was issued (if available). |
message | %(message)s | The logged message, computed as msg % args. This is set when Formatter.format() is invoked. |
module | %(module)s | Module (name portion of filename). |
msecs | %(msecs)d | Millisecond portion of the time when the LogRecord was created. |
msg | You 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)s | Name of the logger used to log the call. |
pathname | %(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
process | %(process)d | Process ID (if available). |
processName | %(processName)s | Process name (if available). |
relativeCreated | %(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
stack_info | You 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)d | Thread ID (if available). |
threadName | %(threadName)s | Thread 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