当使用多个具有不同日志级别的处理程序时,出现意外的python记录器输出 [英] Unexpected python logger output when using several handlers with different log levels

查看:102
本文介绍了当使用多个具有不同日志级别的处理程序时,出现意外的python记录器输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据记录到stderr并记录到文件中.该文件应包含 all 日志消息,对于stderr,应仅使用命令行上配置的日志级别.在日志记录操作方法中对此进行了多次描述-但它似乎对我不起作用.我创建了一个小的测试脚本来说明我的问题:

I am trying to log data to stderr and into a file. The file should contain all log messages, and to stderr should go only the log level configured on the command line. This is described several times in the logging howto - but it does not seem to work for me. I have created a small test script which illustrates my problem:

#!/usr/bin/env python

import logging as l

l.basicConfig(level=100)
logger = l.getLogger("me")

# ... --- === SEE THIS LINE === --- ...
logger.setLevel(l.CRITICAL)

sh = l.StreamHandler()
sh.setLevel(l.ERROR)
sh.setFormatter(l.Formatter('%(levelname)-8s CONSOLE %(message)s'))
logger.addHandler(sh)

fh = l.FileHandler("test.dat", "w")
fh.setLevel(l.DEBUG)
fh.setFormatter(l.Formatter('%(levelname)-8s    FILE %(message)s'))
logger.addHandler(fh)

logger.info("hi this is INFO")
logger.error("well this is ERROR")

在第5行代码行中,我可以使用logger.setLevel(l.CRITICAL)logger.setLevel(l.DEBUG).两种结果都不令人满意.

In line 5th code line I can go for logger.setLevel(l.CRITICAL) or logger.setLevel(l.DEBUG). Both results are unsatisfying.

有了logger.setLevel(l.CRITICAL),我得到了...

With logger.setLevel(l.CRITICAL) I get ...

$ python test.py
$ cat test.dat  
$

现在有了logger.setLevel(l.DEBUG),我得到了...

Now with logger.setLevel(l.DEBUG) I get ...

$ python test.py
INFO:me:hi this is INFO
ERROR    CONSOLE well this is ERROR
ERROR:me:well this is ERROR
$ cat test.dat  
INFO        FILE hi this is INFO
ERROR       FILE well this is ERROR
$

在一种情况下,我什么都看不到,在另一种情况下我什么都看不到,并且一条消息在控制台上甚至显示了两次.

In one case I see nothing nowhere, in the other I see everything everywhere, and one message is being displayed even twice on the console.

现在我可以得到ERROR CONSOLEERROR FILE输出的来源.我知道INFO:me...ERROR:me...输出的来源,我想摆脱它们.

Now I get where the ERROR CONSOLE and ERROR FILE outputs come from, those I expect. I don't get where the INFO:me... or ERROR:me... outputs are coming from, and I would like to get rid of them.

我已经尝试过的事情:

  • Creating a filter as described here: https://stackoverflow.com/a/7447596/902327 (does not work)
  • Emptying handlers from the logger with logger.handlers = [] (also does not work)

有人可以帮我吗?这似乎是一个简单的要求,但我似乎真的没有得到.

Can somebody help me out here? It seems like a straightforward requirement and I really don't seem to get it.

推荐答案

您可以将根级别设置为DEBUG,将传播级别设置为False,然后为其他处理程序设置适当的级别.

You can set the root level to DEBUG, set propagate to False and then set the appropriate level for the other handlers.

import logging as l

l.basicConfig()
logger = l.getLogger("me")

# ... --- === SEE THIS LINE === --- ...
logger.setLevel(l.DEBUG)
logger.propagate = False
sh = l.StreamHandler()
sh.setLevel(l.ERROR)

sh.setFormatter(l.Formatter('%(levelname)-8s CONSOLE %(message)s'))
logger.addHandler(sh)

fh = l.FileHandler("test.dat", "w")
fh.setLevel(l.INFO)
fh.setFormatter(l.Formatter('%(levelname)-8s    FILE %(message)s'))
logger.addHandler(fh)

logger.info("hi this is INFO")

logger.error("well this is ERROR")

输出:

~$ python test.py
ERROR    CONSOLE well this is ERROR
~$ cat test.dat
INFO        FILE hi this is INFO
ERROR       FILE well this is ERROR

这篇关于当使用多个具有不同日志级别的处理程序时,出现意外的python记录器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆