记录setLevel被忽略 [英] Logging setLevel is being ignored

查看:82
本文介绍了记录setLevel被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是从文档中复制的.我应该能够查看所有信息日志.但是我没有.即使将setLevel设置为INFO,我也只能看到警告和以上内容.

The below code is copied from the documentation. I am supposed to be able to see all the info logs. But I don't. I am only able to see the warn and above even though I've set setLevel to INFO.

为什么会这样? foo.py:

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

输出:

workingDirectory$ python foo.py
warn message
error message
critical message

信息和调试消息去哪里了?

Where did the info and debug messages go??

推荐答案

替换行

logger.setLevel(logging.DEBUG)

使用

logging.basicConfig(level=logging.DEBUG, format='%(message)s')

,它应该可以正常工作.如果您不使用任何处理程序来配置日志记录(如您的帖子中所述-您仅为记录器配置一个级别,但在任何地方都没有处理程序),则将获得一个内部处理程序设置为仅在WARNING级别输出消息(不带其他格式)

and it should work as expected. If you don't configure logging with any handlers (as in your post - you only configure a level for your logger, but no handlers anywhere), you'll get an internal handler "of last resort" which is set to output just the message (with no other formatting) at the WARNING level.

这篇关于记录setLevel被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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