[loggers]
keys=root
[handlers]
keys=consoleHandler,errorHandler,debugHandler
[formatters]
keys=errorFormatter,debugFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler,errorHandler,debugHandler
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=debugFormatter
args=(sys.stdout,)
[handler_errorHandler]
class=FileHandler
level=ERROR
formatter=errorFormatter
args=('errors.log',)
[handler_debugHandler]
class=FileHandler
level=DEBUG
formatter=debugFormatter
args=('debugs.log',)
[formatter_errorFormatter]
style={
format=
{asctime} - {name} - {levelname} - line {lineno} - {message}
datefmt=%d/%m/%y - %H:%M:%S
[formatter_debugFormatter]
style={
format={asctime} - {name} - {levelname} - {message}
datefmt=%d/%m/%y - %H:%M:%S
如果我有一个像上面这样的配置文件,我如何访问debugHandler
以便像debugHandler.addFilter(my_custom_filter)
那样做?你知道吗
我基本上是想让DEBUG
消息被发送到debugs.log
并且ERROR
消息被发送到errors.log
,但是目前ERROR
消息也被发送到debugs.log
,这是我不想要的。现在,基于this StackOverflow post,这可以通过过滤器实现,但是为了添加过滤器,我需要一个logging.Handler
对象,我不知道如何获得一个。你知道吗
提前感谢:~)
是的,在调试处理程序上需要一个过滤器,但是不能通过fileConfig添加过滤器。你知道吗
从文档中:
因此,考虑使用dictConfig(不管您的配置如何,都不应该有任何理由不切换到
dictConfig
,因为它更好、更新、更灵活,而且将来所有的好东西都会被添加进来)。你知道吗因此,转换到
dictConfig
的配置看起来像这样(可以按原样粘贴和测试):将输出:
如果您出于某种神秘的原因(我看不到任何原因)坚持使用旧的API,
fileConfig
,请参阅this answer,它使用自定义格式化程序,实现同样的功能,没有过滤器。你知道吗相关问题 更多 >
编程相关推荐