简体   繁体   English

根记录器从非根记录器调用文件处理程序 - 我试图理解为什么

[英]root logger invokes filehandler from non-root logger - I'm trying to understand why

So I have a logging dictconfig like so所以我有一个像这样的日志记录dictconfig

import logging.handlers
from logging.config import dictConfig



LOGGING_DIR = '/homedir/LOGS'

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'loggers': {
        '': {  # root logger - logs to sys.stdout
            'level': 'NOTSET',
            'handlers': ['rootLoggerStreamHandler'],
        },
        'opslog': {  # logs modules in their own logfile
            'level': 'NOTSET',
            'handlers': ['opsLogFileHandler'],
            'propagate': False
        }
    },
    'handlers': {
        'rootLoggerStreamHandler': {
            'level': 'DEBUG',
            'formatter': 'simpleFormatter',
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',  # Default is stderr
        },
        'opsLogFileHandler': {
            'level': 'INFO',
            'formatter': 'simpleFormatter',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': f'{LOGGING_DIR}/opslog.log',
            'mode': 'a',
            'maxBytes': 200000000,
            'backupCount': 2,
            'encoding': None,
            'delay': False
        },
    },
    'formatters': {
        'simpleFormatter': {
            'format': '%(asctime)s: %(filename)s: module:%(funcName)s: %(levelname)s: %(message)s'
        },
    },

}

and I wrote a testscript like so:我写了一个这样的测试脚本:

import logging
import logging.config
import logger_config # file that contains a dict with logger config.


logging.config.dictConfig(logger_config.LOGGING_CONFIG)
logger = logging.getLogger() # will get the root logger


logger.info("this is a test")

when I run the script, 2 things happen当我运行脚本时,发生了两件事

  1. on stdout, I get a log message.在标准输出上,我收到一条日志消息。 This is as expected这和预期的一样
2021-12-19 20:49:06,428: streamhandler_test.py: module:<module>: INFO: this is a test
  1. in /homedir/LOGS an empty file opslog.log is created.在 /homedir/LOGS 中创建了一个空文件 opslog.log。 This is (for me) unexpected.这(对我来说)是出乎意料的。

the "opslog.log" is clearly created by the opsLogFileHandler So my script invokes the "opsLogFileHandler" somehow. “opslog.log”显然是由 opsLogFileHandler 创建的,所以我的脚本以某种方式调用了“opsLogFileHandler”。

I guess it has something to do with inheritance, but as I invoke the root logger, I see no reason why the unrelated opsLogFileHandler (which is only bound to the 'opslog' logger) should create an empty file.我猜它与 inheritance 有关,但是当我调用根记录器时,我看不出为什么不相关的 opsLogFileHandler(仅绑定到“opslog”记录器)应该创建一个空文件。

Also, if I understand inheritance correctly, there's a child -> parent relation from any non-root logger to the root logger (unless propagation is set to False), but not the other way around.此外,如果我正确理解 inheritance ,则从任何非根记录器到根记录器都有一个子 -> 父关系(除非传播设置为 False),但反之则不然。

I"v tried finding an answer reading the docs, but frankly, doc is complex, and I can't work it out.我尝试通过阅读文档找到答案,但坦率地说,文档很复杂,我无法解决。

Any explanation on why that file "opslog.log' is created is very welcome任何关于为什么创建该文件“opslog.log”的解释都非常受欢迎

The opslog.log file is created when you create the opsLogFileHandler handler, because the delay parameter is set to False in the configuration for it. opslog.log文件是在您创建opsLogFileHandler处理程序时创建的,因为delay参数在其配置中设置为False (If delay is True , the file isn't opened until the first time something needs to be written to it.) (如果delayTrue ,文件直到第一次需要写入文件时才会打开。)

The handler is created when you do the configuration.处理程序是在您进行配置时创建的。 So as soon as you call dictConfig() , that opslog.log file will be opened for append, and, if it doesn't exist, created as an empty file ready to be written to.因此,一旦您调用dictConfig() ,将为 append 打开该opslog.log文件,如果它不存在,则创建为准备写入的空文件。

just tried your solution, and that worked like a charm.刚刚尝试了您的解决方案,这就像一个魅力。 As always, the devil is in the details.一如既往,魔鬼在细节中。 Thank you very much for helping me out !!非常感谢你帮助我!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM