logging remove / inspect / modify handlers configu

2019-03-17 02:28发布

How can I remove / inspect / modify handlers configured for my loggers using the fileConfig() function?

For removing there is Logger.removeHandler(hdlr) method, but how do I get the handler in first place if it was configured from file?

2条回答
男人必须洒脱
2楼-- · 2019-03-17 03:07

logger.handlers contains a list with all handlers of a logger.

查看更多
孤傲高冷的网名
3楼-- · 2019-03-17 03:14

Another approach might be to use a JSON or YAML config file which gets loaded into a dictionary which you can then view/manipulate before it's passed to the logger.config.

import yaml
import logging.config

with open (LOG_CONFIG, 'rt') as f:
   config=yaml.safe_load(f)
   config['handlers']['error_file_handler']['filename']='foo'
logging.config.dictConfig(config)
查看更多
登录 后发表回答