【问题标题】:Which module should contain logging.config.dictConfig(my_dictionary)? What about my_dictionary?哪个模块应该包含 logging.config.dictConfig(my_dictionary)? my_dictionary 呢?
【发布时间】:2013-07-29 21:32:51
【问题描述】:

这都是 Python。我还在学习中……

我为我的 Python 应用程序编写了两个模块:Module1Module2。 我需要将我的日志记录结果发送到三个不同的文件。 Module1 发送到 setup.logsetupdetails.log 文件和 Module2 发送到 runall.log Module2 是我运行的应用程序。其中有一个调用 Module1 的导入语句。

因为我已经在my_dictionary 中配置了我的日志记录,所以哪个模块应该包含字典?哪个模块应该包含logging.config.dictConfig(my_dictionary) 函数?

你知道我在哪里可以找到一个很好的脚本以及如何使用dictConfig 的例子吗?

【问题讨论】:

    标签: python logging dictionary module


    【解决方案1】:

    所以,我终于想通了。

    如果将字典放在子模块Module 1(并设置Propagate: True)中,一切都会很好。

    MY_DICTIONARY = {
    'version': 1,              
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)-8s %(asctime)s %(module)s %(process)d %(thread)d %(message)s',
            'datefmt': '%a, %d %b %Y %H:%M:%S'
    
        },
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
            'datefmt': '%a, %d %b %Y %H:%M:%S'
    
        },
        'simple': {
            'format': '%(asctime)s %(levelname)-8s %(message)s',
            'datefmt': '%a, %d %b %Y %H:%M:%S'
        }
    #etc.
    }
    

    }

    随后是以下调用:

    logging.config.dictConfig(MY_DICTIONARY)
    vmrunalllogger = logging.getLogger('VMRunAll_Python_Logger')
    

    然后在模块 2(父模块)中,有这个:

    logging.config.dictConfig(MY_DICTIONARY)
    mylogger = logging.getLogger('RunAll_Logger')
    

    我找不到显示两个不同模块记录到多个文件的具体示例,就像我正在做的那样,但来自多个来源的信息(如下所示)有所帮助:

    1. From the Python documentation
    2. Another question on Stackoverflow.com
    3. And the examples of in the cookbook

    【讨论】:

    • “VMRunAll_Python_Logger”是在配置的“记录器”部分中定义的,还是只是模块名称?
    【解决方案2】:

    对于dictConfig:

    import logging.config
    logging.config.dictConfig() #Your method.
    

    就您需要做的事情而言,如果您发布一些代码可能会更容易。

    Some documentation

    An Example

    【讨论】:

    • 感谢您对 import logging.config 的确认,我昨晚在一天结束时尝试了我的代码,并收到了 AttributeError: 'module' object has no attribute 'config' 消息。您建议导入的恰好是AttributeError 的解决方案。至于例子,我已经看过了。不过谢谢。让我马上回来带新消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2012-08-26
    • 2021-01-10
    • 2016-08-04
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多