簡體   English   中英

Python的logging.config.dictConfig()是否應用記錄器的配置設置?

[英]Does Python's logging.config.dictConfig() apply the logger's configuration settings?

我一直在嘗試實現一個基本的記錄器,該記錄器可以在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運行時, python3 logger.py產生輸出(在終端中);

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

第一; 查看Python的日志記錄級別 30是默認的日志記錄級別“警告”。 這與我在處理程序和根記錄器中設置的兩個level屬性的設置相矛盾。 似乎JSON不正確,或者我錯過了應用它的函數調用。

第二; 該線程使我認為,盡管我使用dictConfig()調用加載了配置,但仍需要通過logger.py文件中的進一步調用將其應用於日志記錄。 擁有配置,然后無論如何都要冗長地應用每個設置,似乎有點多余。

另外; 當我嘗試使用配置文件格式時,它按照我的預期工作。 即; 通過一個函數調用加載文件,並能夠立即進行日志記錄調用。 這是令人困惑的,因為與使用JSON或YAML的dictConfig()相比,與此格式一起使用的較早的fileConfig()調用為什么會提供更簡化的功能?

最終,我有點困惑,想解決這個問題。 感謝您的寶貴時間和幫助。

編輯:從Alex.P的評論中,我將以下處理程序添加到log_config.json並將其根更改為該處理程序。

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

檢查輸出,與上面相同。

啊,我找出了問題所在。 原來是JSON。 這個基於我的工作的示例中,它在JSON中具有一個額外的logging屬性,該屬性封裝了所有記錄器,處理程序等。

刪除該屬性並使層次結構更像是YAML文件(我也對其進行了測試,並且可以正常工作),它可以按預期工作。 我甚至可以刪除的額外通話basicConfiglogger.py

最終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"
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM