Python 2.7 python日志记录模块-终端、stdout、stderr
我想使用日志模块将python(2.7)脚本的输出记录到三个流中:Python 2.7 python日志记录模块-终端、stdout、stderr,python-2.7,logging,Python 2.7,Logging,我想使用日志模块将python(2.7)脚本的输出记录到三个流中: 至终端/标准输出(根记录器) 到包含所有消息的日志文件(完整记录器,类似于stdout) 到只包含警告和错误消息的日志文件(错误记录器,类似于stderr) 伐木工人2。三,。需要旋转文件处理程序(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
调试::调试消息
[2017-12-13 15:18:59.265]信息::信息消息
[2017-12-13 15:18:59.265]警告::警告信息
[2017-12-13 15:18:59.265]错误::错误消息
[2017-12-13 15:18:59.265]错误::异常消息
回溯(最近一次呼叫最后一次):
文件“C:\path\test\u logger2.py”,第12行,在
b=a[0]
索引器:列表索引超出范围
标准日志
[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
调试::调试消息
[2017-12-13 15:18:59.265]信息::信息消息
[2017-12-13 15:18:59.265]警告::警告信息
[2017-12-13 15:18:59.265]错误::错误消息
[2017-12-13 15:18:59.265]错误::异常消息
回溯(最近一次呼叫最后一次):
文件“C:\path\test\u logger2.py”,第12行,在
b=a[0]
索引器:列表索引超出范围
标准日志
[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
[2017-12-13 15:18:59.265]警告::警告消息
[2017-12-13 15:18:59.265]错误::错误消息
[2017-12-13 15:18:59.265]错误::异常消息
回溯(最近一次呼叫最后一次):
文件“C:\path\test\u logger2.py”,第12行,在
b=a[0]
索引器:列表索引超出范围
必须在一个根记录器下组合三个处理程序:
[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
刚刚找到解决方案,发布为答案