2
我對記錄器使用以下dictConfig。但是,我無法在運行時修改日誌記錄級別。 log_config.json的Python日誌覆蓋dictConfig級別
#contents
{
"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"]
}
}
然後我得到記錄器,並使用設置級別:
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)
但是,由於記錄器使用dictConfig創建我不是能夠覆蓋的水平。我想建立一個UI工具,它有一個選項菜單,可以在運行時調整日誌級別,而不必破解開放代碼或json文件進行調整。我可以調整水平較高,但由於某種原因,它不會讓水平更低...
我想要做的是將info_file和控制檯處理程序設置爲INFO(20)在配置中,然後可以選擇在運行時將它們更改爲DEBUG(10)。有任何想法嗎?