[html]
Level Numeric value CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 NOTSET 0例如配置文件中level定义为WARN,则INFO, DEBUG,NOTSET三个级别的日志点则不会输出,很方便的做到了日志级别控制。
args定义了日志方件名,写方式,最大大小,保存最多个数等属性。
2.编码,测试
[python]
#!/usr/bin/env python # -*- coding: utf-8 -*- import logging import logging.config #日志初始化 LOG_FILENAME = 'logging.conf' logging.config.fileConfig(LOG_FILENAME) logger = logging.getLogger("simple_log_example") #测试代码 logger.debug("debug message") logger.info("info message") logger.warn("warn message") logger.error("error message") logger.critical("critical message") 运行后,查看日志文件,内容如下:
[plain]
[2012-02-11 14:47:05,483 - simple_log_example]DEBUG: debug message - test_log.py:48 [2012-02-11 14:47:05,483 - simple_log_example]INFO: info message - test_log.py:49 [2012-02-11 14:47:05,483 - simple_log_example]WARNING: warn message - test_log.py:50 [2012-02-11 14:47:05,483 - simple_log_example]ERROR: error message - test_log.py:51 [2012-02-11 14:47:05,483 - simple_log_example]CRITICAL: critical message - test_log.py:52 如将日志级别设置为WARN,再次运行,查看日志:
[plain]
[2012-02-11 14:54:20,046 - simple_log_example]WARNING: warn message - test_log.py:50 [2012-02-11 14:54:20,092 - simple_log_example]ERROR: error message - test_log.py:51 [2012-02-11 14:54:20,092 - simple_log_example]CRITICAL: critical message - test_log.py:52 附一:format参数格式说明:
[plain]
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.