【问题标题】:python logging module - terminal, stdout, stderrpython 日志记录模块 - 终端、标准输出、标准错误
【发布时间】:2025-04-20 16:20:02
【问题描述】:

我想使用日志模块将 python (2.7) 脚本的输出记录到三个流中:

  1. 到终端/标准输出(根记录器)
  2. 到包含所有消息的日志文件(完整记录器,类似于标准输出)
  3. 到仅包含警告和错误消息的日志文件(错误记录器,类似于 stderr)

Loggers 2. 和 3. 需要 RotatingFileHandlers (maxBytes: 1024, backupCount: 3),一段时间后覆盖旧的历史日志文件。

通过以下设置,它成功地将日志以所需格式写入终端,并创建了 stdout.log 和 stderr.log 文件,但两个文件都显示为空。

关于我缺少正确填充日志文件的任何提示?


这是测试脚本

test_logger.py

import logging.config

logging.config.fileConfig('logging.conf') 

logging.debug("DEBUG MESSAGE")
logging.info("INFO MESSAGE")
logging.warning("WARNING MESSAGE")
logging.error("ERROR MESSAGE")

a = []
try :
    b = a[0]
except :
    logging.exception("EXCEPTION MESSAGE")
# end try

这是记录器的配置

logging.conf

[loggers]
keys=root,fullLogger,errorLogger

[handlers]
keys=rootHandler,fullHandler,errorHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=rootHandler
propagate=1

[logger_fullLogger]
level=DEBUG
handlers=fullHandler
qualname=fullLogger
propagate=1

[logger_errorLogger]
level=WARNING
handlers=errorHandler
qualname=errorLogger
propagate=1

[handler_rootHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_fullHandler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('stdout.log','w',1024,3)

[handler_errorHandler]
class=logging.handlers.RotatingFileHandler
level=WARNING
formatter=simpleFormatter
args=('stderr.log','w',1024,3)

[formatter_simpleFormatter]
format=[%(asctime)s.%(msecs)03d] %(levelname)s :: %(message)s
datefmt=%Y-%m-%d %H:%M:%S

我希望得到以下输出:

终端

[2017-12-13 15:18:59.265] DEBUG :: DEBUG MESSAGE
[2017-12-13 15:18:59.265] INFO :: INFO MESSAGE
[2017-12-13 15:18:59.265] WARNING :: WARNING MESSAGE
[2017-12-13 15:18:59.265] ERROR :: ERROR MESSAGE
[2017-12-13 15:18:59.265] ERROR :: EXCEPTION MESSAGE
Traceback (most recent call last):
  File "C:\path\test_logger2.py", line 12, in <module>
    b = a[0]
IndexError: list index out of range

stdout.log

[2017-12-13 15:18:59.265] DEBUG :: DEBUG MESSAGE
[2017-12-13 15:18:59.265] INFO :: INFO MESSAGE
[2017-12-13 15:18:59.265] WARNING :: WARNING MESSAGE
[2017-12-13 15:18:59.265] ERROR :: ERROR MESSAGE
[2017-12-13 15:18:59.265] ERROR :: EXCEPTION MESSAGE
Traceback (most recent call last):
  File "C:\path\test_logger2.py", line 12, in <module>
    b = a[0]
IndexError: list index out of range

stderr.log

[2017-12-13 15:18:59.265] WARNING :: WARNING MESSAGE
[2017-12-13 15:18:59.265] ERROR :: ERROR MESSAGE
[2017-12-13 15:18:59.265] ERROR :: EXCEPTION MESSAGE
Traceback (most recent call last):
  File "C:\path\test_logger2.py", line 12, in <module>
    b = a[0]
IndexError: list index out of range

【问题讨论】:

  • 刚刚找到解决方案,作为答案发布

标签: python-2.7 logging


【解决方案1】:

必须将三个处理程序组合在一个根记录器下:

[loggers]
keys=root

[handlers]
keys=rootHandler,fullHandler,errorHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=NOTSET
handlers=rootHandler,fullHandler,errorHandler

##### TERMINAL HANDLER #####
# Setup for output to terminal
# level=DEBUG : Logging ALL messages

[handler_rootHandler]
class=logging.StreamHandler
level=DEBUG
formatter=simpleFormatter
args=tuple()

##### STDOUT HANDLER #####
# Setup for output to stdout.log file
# level=DEBUG : Logging ALL messages
# arg=2097152 : RotatingFileHandler with 2097152 Bytes (2 MB) max size
# arg=3 : RotatingFileHandler with 3 backup files

[handler_fullHandler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('stdout.log','w',2097152,3)

##### STDERR HANDLER #####
# Setup for output to stderr.log file
# level=WARNING : Logging WARNING,ERROR and EXCEPTION messages
# arg=2097152 : RotatingFileHandler with 2097152 Bytes (2 MB) max size
# arg=3 : RotatingFileHandler with 3 backup files

[handler_errorHandler]
class=logging.handlers.RotatingFileHandler
level=WARNING
formatter=simpleFormatter
args=('stderr.log','w',2097152,3)

##### APPEARANCE #####
# Setup for formatting of log message
# e.g. [2017-12-13 16:00:50.983] DEBUG :: ext_module : DEBUG MESSAGE
# e.g. [2017-12-13 16:00:50.983] INFO :: ext_module : INFO MESSAGE
# e.g. [2017-12-13 16:00:50.983] WARNING :: ext_module : WARNING MESSAGE
# e.g. [2017-12-13 16:00:50.983] ERROR :: ext_module : ERROR MESSAGE
# e.g. [2017-12-13 16:00:50.983] ERROR :: ext_module : EXCEPTION MESSAGE

[formatter_simpleFormatter]
format=[%(asctime)s.%(msecs)03d] %(levelname)s :: %(module)s : %(message)s
datefmt=%Y-%m-%d %H:%M:%S

【讨论】: