简体   繁体   English

Python Logger不遵守日志级别

[英]Python Logger not respecting log level

Setup: Python 3.3 设定:Python 3.3

I have a unit test program that I want to print out log messages. 我有一个要打印日志消息的单元测试程序。 Python is not printing anything but WARNING or higher messages through this logger I made, even though I set the log severity to INFO. 即使我将日志严重性设置为INFO,Python也不会通过我创建的该记录器打印任何警告或更高级别的消息。

Here is the code I use: 这是我使用的代码:

test_logger = logging.getLogger('DesktopprTester')
print(test_logger.getEffectiveLevel())
test_logger.setLevel(logging.INFO)
print(test_logger.getEffectiveLevel())
test_logger.warning('WARN')
test_logger.info('INFO')

And here is the output: 这是输出:

30 <-30 is WARN
20 <-20 is INFO
WARN

It does not print INFO. 它不打印信息。 So I am missing all my info statements in this file... and I am not sure why. 因此,我在此文件中丢失了所有我的信息语句...而且我不确定为什么。 The docs seem to agree with me. 这些文档似乎同意我的观点。 When I used to use the root logger, and set the level to info, it would print it (And all the other stuff it was doing). 当我以前使用root记录器并将级别设置为info时,它将打印它(以及它正在执行的所有其他操作)。 Now that I made my own logger, it doesn't seem to work. 现在,我已经创建了自己的记录器,它似乎不起作用。 Note that WARNING level messages still come up, but they aren't what I want to see. 请注意,警告级别的消息仍然会出现,但是我不想看到这些消息。

Is there something I am missing here? 我在这里缺少什么吗? I feel like there's some obvious answer I'm not seeing... 我觉得我看不到一些明显的答案...

While running your code in python 2, you get a clear warning: 在python 2中运行代码时,您会收到明确的警告:

No handlers could be found for logger "DesktopprTester"

So you simply need to add a handler to your logger: 因此,您只需要向记录器添加处理程序即可:

test_logger = logging.getLogger('DesktopprTester')
hdlr = logging.StreamHandler()
test_logger.addHandler(hdlr)

The warning doesn't appear on python 3 - I'm not sure why 该警告未出现在python 3上-我不确定为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM