Django中的日志系统章
Python日志系统
Django使用logging
模块记录日志。Python的日志系统分为4块。分别是:loggers
,handlers
,filters
和formatters
。
- loggers
- handlers
- filtersDjango
- formatters
logger
logger模块是日志系统的入口,首先处理进入日志系统的消息。每个logger实例都有一个名字,不同名字的loggger实例按不同策略处理日志消息。
import logging
LOG = logging.getLogger('Django')
LOG.debug("This is debug info.")
每个logger实例都有一个默认等级,只有当消息的等级等于或超过默认等级,才会被处理。否则,消息就被丢弃。
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
当logger模块接收了消息以后,会传给handler模块。
handler
handler模块决定日志保存在哪里,是在终端打印、保存在本地文件、还是保存在日志服务器。
handler实例也有默认等级,只有当消息的等级等于或超过默认等级,才会被处理。否则,消息就被丢弃。
一个logger实例可以有多个handler实例,每个handler实例处理不同等级的消息。
filter
默认情况下,logger模块只检查消息等级。在消息从logger模块传输到handler模块的过程中,filter模块可以对消息进行高级筛选。
比如,filter实例可以筛选从指定源发出的ERROR消息。
filter实例也可以改变消息的等级,比如,在某些条件下,把ERROR消息降级成WARNING。
filter实例可以与logger实例或handler实例关联,也可以把多个filter实例做成筛选链,做精确筛选。
formatter
formatter模块指定日志的格式。
各个模块关系如下
loggers -> filters -> handlers -> formatters -> [stdio, file, network]
在django中设置日志
在setting.py文件中,配置以下两个变量。
LOGGING_CONFIG = 'logging.config.dictConfig'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '[%(process)d] [%(thread)d] %(asctime)s.%(msecs)-3d %(levelname)s %(message)s',
'datefmt': '%H:%M:%S'
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/tmp/webconsole.log',
'formatter': 'simple',
},
},
'loggers': {
'Django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
在需要记录LOG的文件中,通过Loggers记录日志。名字为setting.py文件中设置的Logger名字。
import logging
LOG = logging.getLogger('Django')
LOG.warning();
日志格式
参考python日志格式。
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). |
module | %(module)s | Module (name portion of filename). |
msecs | %(msecs)d | Millisecond portion of the time when the LogRecord was created. |
message | %(message)s | The logged message, computed as msg % args. This is set when Formatter.format() is invoked. |
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). |