簡體   English   中英

更改python fileConfig記錄器的級別

[英]Change level of python fileConfig logger

我有一個從文件配置的記錄器,並希望更改我的日志記錄級別,而無需更改.conf文件,而是使用內聯代碼;

import logging.config

logging.config.fileConfig('..\\LoggingConfig\\loggingfile.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

使用logging.config.fileConfig並且您希望一次動態更改所有子記錄器的級別時,您可以...

a) 設置 root logger的 級別

logging.getLogger().setLevel(logging.WARNING)

b) 禁用其他級別

logging.disable(logging.INFO)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM