加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

更改python fileConfig记录器的级别

发布时间:2020-12-20 12:20:00 所属栏目:Python 来源:网络整理
导读:我有一个从文件配置的记录器,并希望更改我的日志记录级别,而无需更改.conf文件,而是使用内联代码; import logging.configlogging.config.fileConfig('..LoggingConfigloggingfile.conf')logging.StreamHandler.setLevel(logging.info)logging.debug("Deb
我有一个从文件配置的记录器,并希望更改我的日志记录级别,而无需更改.conf文件,而是使用内联代码;

import logging.config

logging.config.fileConfig('..LoggingConfigloggingfile.conf')

logging.StreamHandler.setLevel(logging.info)

logging.debug("Debug")
logging.info("Info")

这应该只将“信息”日志行打印到屏幕上.我不知道在哪个对象上调用setLevel()! logging.StreamHandler.setLevel(logging.info)在30分钟搜索后只是在黑暗中刺伤……

loggingfile.conf文件;

[loggers]
keys=root

[logger_root]
handlers=screen
level=NOTSET

[formatter_modfunc]
format=%(module)-20s  %(funcName)-25s %(levelno)-3s: %(message)s

[handlers]
keys=screen

[handler_screen]
class=StreamHandler
formatter=modfunc
level=DEBUG
args=(sys.stdout,)
qualname=screen

解决方法

您需要在Logger实例上调用setLevel.

LOGGER = logging.getLogger('your.module.file.name')
LOGGER.setLevel(_level)
LOGGER.info('foo')

如果您只使用基本记录器,则可以这样做

logging.basicConfig(level=_level)
logging.info('foo')

见http://docs.python.org/howto/logging.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读