python - Python日志记录不输出调试消息
问题描述
我正在尝试使用 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
文件中?
解决方案
您需要将根记录器的级别设置为 DEBUG 而不是 INFO。
"root": {
"level": "DEBUG",
推荐阅读
- sql - 使用多个命名空间不工作的更新 XML 值
- php - 在 codeception 中加载 params.php 文件不起作用
- github - 使用 GitHub Pages 提供文件(即 zip)
- scala - 在 Spark 2.3 中将元素添加到结构数组并合并结构数组
- javascript - 这段代码是如何工作的,我没有清楚地理解?
- php - 如何检查给定键值对的多维数组,即。完全相同的键和值?
- java - 在 Android Studio 3.5.1 中这是什么类型的问题?
- php - 文件上传在 AWS Linux 2 服务器中不能超过 10mb?
- c# - 使用 JsonSerializer.DeserializeAsync 反序列化 JSON 不使用我的 JsonConverter
- android - 无法在android中进行无线调试(ADB)