[英]Python logging override dictConfig level
I am using the following dictConfig for a logger. 我使用以下dictConfig作为记录器。 However, I am unable to modify the logging level at runtime. 但是,我无法在运行时修改日志记录级别。
#contents of log_config.json #contents of log_config.json
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
},
"detailed": {
"format": "%(asctime)s %(name)-12s %(module)-17s line:%(lineno)-4d %(levelname)-8s %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "simple",
"stream": "ext://sys.stdout"
},
"info_file_handler": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "INFO",
"formatter": "detailed",
"filename": "info.log",
"when": "midnight",
"backupCount": 7,
"encoding": "utf8"
},
"error_file_handler": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "ERROR",
"formatter": "detailed",
"filename": "errors.log",
"when": "midnight",
"backupCount": 7,
"encoding": "utf8"
}
},
"loggers": {
"": {
"level": "ERROR",
"handlers": ["console"],
"propagate": "no"
}
},
"root": {
"level": "NOTSET",
"handlers": ["console", "info_file_handler", "error_file_handler"]
}
}
I then get the logger and set the level using: 然后我得到记录器并使用以下方法设置级别:
with open('/path/to/log_config.json', 'r') as fd:
cfg = json.load(fd)
logging.config.dictConfig(cfg)
logger = logging.getLogger(__name__)
logger.setLevel(10)
But, because the logger was created using the dictConfig I am not able to override the levels. 但是,因为记录器是使用dictConfig创建的,所以我无法覆盖级别。 I would like to build a UI tool which has an option menu to adjust the logging levels at runtime without having to crack open code or the json file to make adjustments. 我想构建一个UI工具,它有一个选项菜单,可以在运行时调整日志记录级别,而无需破解开放代码或json文件进行调整。 I am able to adjust the level higher, but for some reason it will not let the level go lower... 我能够调高水平,但由于某种原因它不会让水平降低......
What I would like to do is set the info_file and console handlers to INFO (20) in the config, and then have the option to change them to DEBUG (10) at runtime. 我想要做的是在配置中将info_file和控制台处理程序设置为INFO(20),然后可以选择在运行时将它们更改为DEBUG(10)。 Any ideas? 有任何想法吗?
I had a similar problem, and just figured it out. 我有一个类似的问题,只是想出来了。
In my case I only wanted to change the level for the "console" handler, but you can easily extend this to all handlers and try it out: 在我的情况下,我只想更改“控制台”处理程序的级别,但您可以轻松地将其扩展到所有处理程序并尝试它:
logger = logging.getLogger(__name__)
for handler in logger.handlers:
if handler.get_name() == 'console':
handler.setLevel(logging.DEBUG)
(note, I copied your __name__
, but for me I'm using an actual string to name the logger - just in case that matters) (注意,我复制了你的__name__
,但对我来说,我使用一个实际的字符串来命名记录器 - 以防万一)
I was looking for a dict config and I found your question. 我正在寻找一个dict配置,我找到了你的问题。 I followed your exact code and I also couldn't change the log level then I found this answer . 我按照你的确切代码,我也无法更改日志级别然后我找到了这个答案 。 And instead of __name__
using @Tjorriemorrie 's third solution returned correct logger.handlers
. 而不是__name__
使用@Tjorriemorrie的第三个解决方案返回正确的logger.handlers
。 And I combined with @Starman ' s answer ... Now I can change the logging level wherever I want and whenever I want. 我结合了@Starman的回答 ......现在我可以随时随地改变日志记录级别。
logger = logging.getLogger()
for handler in logger.handlers:
if handler.get_name() == 'console':
handler.setLevel(logging.DEBUG)
PS: Thank you for your code snippet! PS:谢谢你的代码片段! It helped me a lot. 这对我帮助很大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.