【问题标题】:Does Python's logging.config.dictConfig() apply the logger's configuration settings?Python 的 logging.config.dictConfig() 是否应用了记录器的配置设置?
【发布时间】:2016-11-14 09:30:39
【问题描述】:

我一直在尝试实现一个基本的记录器,它可以在 Python 3.5 中写入文件,从 JSON 配置文件加载设置。 我会先展示我的代码;

log_config.json

{
    "version": 1,
    "disable_existing_loggers": "false",
    "logging": {
        "formatters": {
            "basic": {
                "class": "logging.Formatter",
                "style": "%",
                "datefmt": "%I:%M:%S",
                "format": "[%(asctime)] %(levelname:<8s): (name:<4s): %(message)"
            }
        },

        "handlers": {
            "file": {
                "class": "logging.handlers.FileHandler",
                "level": "DEBUG",
                "formatter": "basic",
                "filename": "test.log",
                "mode": "a",
                "encoding": "utf-8"
            }
        },

        "loggers": { },

        "root": {
            "handlers": ["file"],
            "level": "DEBUG"
        }
    }
}

还有logger.py

import json
import logging
import logging.config

logging.basicConfig()
with open("log_config.json", "r") as fd:
    logging.config.dictConfig(json.load(fd))

logger = logging.getLogger()  # Returns the "root" logger
print(logger.getEffectiveLevel())  # Check what level of messages will be shown

logger.debug("Test debug message")
logger.info("Test info message")
logger.warn("Test warning message")
logger.error("Test error message")
logger.critical("Test critical message")

使用python3 logger.py 运行时会产生输出(在终端中);

30
WARNING:root:Test warning message
ERROR:root:Test error message
CRITICAL:root:Test critical message

首先;看着 Python's logging levels。 30 是“警告”的默认日志记录级别。这与我在处理程序和根记录器中设置的level 属性的设置相矛盾。 JSON 似乎不正确,或者我错过了应用它的函数调用。

第二; This thread 让我觉得虽然我使用 dictConfig() 的调用加载了配置,但我仍然需要在我的 logger.py 文件中通过进一步调用将其应用于日志记录。你有配置然后必须详细地应用每个设置似乎有点多余。

另外;当我尝试使用Configuration file format 时,它的工作原理和我想的一样。即;使用一个函数调用加载文件并能够立即进行日志记录调用。这很令人困惑,因为为什么使用这种格式的旧 fileConfig() 调用会比使用 JSON 或 YAML 的 dictConfig() 提供更精简的功能?

最终,我有点困惑,想弄清楚这一点。感谢您的宝贵时间和帮助。

编辑:根据 Alex.P 的评论,我将以下处理程序添加到 log_config.json 并将处理程序的根更改为它。

"console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "basic",
            "stream": "ext://sys.stdout"
        },

查看输出,同上。

【问题讨论】:

  • 我现在无法测试它,但我认为您只设置了日志记录到文件,而不是控制台,所以日志记录可能使用 WARNING 作为日志记录的默认值终点站。尝试添加另一个处理程序,使用 logging.StreamHandler 作为类。
  • @Alex.P 感谢您的意见。我添加了一个输出到控制台的处理程序,不幸的是它给出了相同的输出。请参阅我的编辑以了解我添加到 JSON 文件中的内容。请注意,我打算将日志记录写入文件。

标签: python json python-3.x logging


【解决方案1】:

啊,我知道出了什么问题。原来是JSON。 从我的工作基础this example 开始,它在 JSON 中有一个额外的 logging 属性,它封装了所有记录器、处理程序等。

删除该属性并使层次结构更像 YAML 文件(我也对其进行了测试,并且可以正常工作),它可以按预期工作。我什至可以在我的logger.py 中删除对basicConfig 的额外调用。

最终的 JSON;

{
    "version": 1,
    "disable_existing_loggers": "false",
    "formatters": {
        "basic": {
            "class": "logging.Formatter",
            "datefmt": "%I:%M:%S",
            "format": "%(asctime)s %(levelname)s %(name)s %(message)s"
        }
    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "basic",
            "stream": "ext://sys.stdout"
        },
        "file": {
            "class": "logging.FileHandler",
            "level": "DEBUG",
            "formatter": "basic",
            "filename": "test.log",
            "mode": "w",
            "encoding": "utf-8"
        }
    },

    "loggers": { },

    "root": {
        "handlers": ["console", "file"],
        "level": "DEBUG"
    }
}

【讨论】:

  • 我不知道你可以在 dictconfig 中有一个“root”字段。
  • 我从你那里了解到,必须指定utf-8,以防代码在具有不同字符集的其他机器上运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多