Python日志记录不输出调试消息

时间:2020-01-03 18:38:42

标签: python logging

我正在尝试使用python日志记录模块将消息输出到文件,我在网上找到了一些配置,并根据我的需要对其进行了自定义,但它并没有打印我想要打印的内容。具有DEBUG级别的文件处理程序中没有DEBUG级别的消息。

我正在为记录器使用此JSON配置:

{
  "version": 1,
  "formatters": {
    "simple": {
      "format": "%(asctime)s : %(levelname)-8s - %(name)s : %(message)s",
      "datefmt": "%Y-%m-%d %H:%M:%S"
    }
  },
  "handlers": {
    "file_handler": {
      "class": "logging.handlers.RotatingFileHandler",
      "level": "DEBUG",
      "formatter": "simple",
      "filename": "info.log",
      "maxBytes": 1E6,
      "backupCount": 5,
      "encoding": "utf8"
    },
    "error_file_handler": {
      "class": "logging.handlers.RotatingFileHandler",
      "level": "ERROR",
      "formatter": "simple",
      "filename": "errors.log",
      "maxBytes": 1E6,
      "backupCount": 5,
      "encoding": "utf8"
    }
  },
  "root": {
    "level": "INFO", # EDIT - change this to DEBUG to fix
    "handlers": ["file_handler", "error_file_handler"]
  }
}

我执行以下代码:

import logging
import logging.config as lconfig

import json

with open('logger.json', 'rt') as f:
    config = json.load(f)
lconfig.dictConfig(config)

logger = logging.getLogger(__name__)

logger.info('info')
logger.warning('warning')
logger.debug('debug')
logger.error('error')
logger.critical('critical')

这里的问题是文件info.log没有调试行,输出是这样的:

2020-01-03 15:32:23 : INFO     - __main__ : info
2020-01-03 15:32:23 : WARNING  - __main__ : warning
2020-01-03 15:32:23 : ERROR    - __main__ : error
2020-01-03 15:32:23 : CRITICAL - __main__ : critical

我应该怎么做才能解决并将调试消息打印到info.log文件中?

1 个答案:

答案 0 :(得分:1)

您需要将根记录器的级别设置为DEBUG而不是INFO。

"root": { 
  "level": "DEBUG",