哪个模块应该包含logging.config.dictConfig(我的字典)?我的字典呢?

2024-05-20 01:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

你知道我在哪里能找到一个好的脚本,并举例说明如何使用dictConfig?在


Tags: 模块文件importlog应用程序dictionary字典my
2条回答

所以,我终于明白了。在

如果把字典放在子模块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.
}

}

接着是以下电话:

^{pr2}$

然后在模块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

对于dictConfig:

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

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

Some documentation

An Example

相关问题 更多 >