在多个模块中使用日志记录 [英] Using logging in multiple modules

查看:36
本文介绍了在多个模块中使用日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的小型python项目 -

I have a small python project that has the following structure -

Project 
 -- pkg01
   -- test01.py
 -- pkg02
   -- test02.py
 -- logging.conf

我计划使用默认的日志记录模块将消息打印到标准输出和日志文件.要使用日志模块,需要进行一些初始化 -

I plan to use the default logging module to print messages to stdout and a log file. To use the logging module, some initialization is required -

import logging.config

logging.config.fileConfig('logging.conf')
logger = logging.getLogger('pyApp')

logger.info('testing')

目前,我在开始记录消息之前在每个模块中执行此初始化.是否可以在一个地方只执行一次初始化,以便通过在整个项目中记录来重复使用相同的设置?

At present, I perform this initialization in every module before I start logging messages. Is it possible to perform this initialization only once in one place such that the same settings are reused by logging all over the project?

推荐答案

最佳实践是,在每个模块中,定义一个记录器:

Best practice is, in each module, to have a logger defined like this:

import logging
logger = logging.getLogger(__name__)

在模块顶部附近,然后在模块中的其他代码中执行例如

near the top of the module, and then in other code in the module do e.g.

logger.debug('My message with %s', 'variable data')

如果您需要在模块内细分日志记录活动,请使用例如

If you need to subdivide logging activity inside a module, use e.g.

loggerA = logging.getLogger(__name__ + '.A')
loggerB = logging.getLogger(__name__ + '.B')

并根据需要登录到 loggerAloggerB.

and log to loggerA and loggerB as appropriate.

在您的主程序或程序中,执行例如:

In your main program or programs, do e.g.:

def main():
    "your program code"

if __name__ == '__main__':
    import logging.config
    logging.config.fileConfig('/path/to/logging.conf')
    main()

def main():
    import logging.config
    logging.config.fileConfig('/path/to/logging.conf')
    # your program code

if __name__ == '__main__':
    main()

请参阅此处以了解从多个模块进行日志记录,以及此处 用于记录将被其他代码用作库模块的代码的配置.

See here for logging from multiple modules, and here for logging configuration for code which will be used as a library module by other code.

更新:在调用 fileConfig() 时,如果您使用的是 Python 2.6 或更高版本,您可能需要指定 disable_existing_loggers=False(请参阅文档了解更多信息).默认值为 True 以实现向后兼容性,这会导致所有现有记录器被 fileConfig() 禁用,除非它们或其祖先在配置中明确命名.将值设置为 False 后,现有记录器将保持不变.如果使用 Python 2.7/Python 3.2 或更高版本,您可能希望考虑 dictConfig() API,它比 fileConfig() 更好,因为它可以更好地控制配置.

Update: When calling fileConfig(), you may want to specify disable_existing_loggers=False if you're using Python 2.6 or later (see the docs for more information). The default value is True for backward compatibility, which causes all existing loggers to be disabled by fileConfig() unless they or their ancestor are explicitly named in the configuration. With the value set to False, existing loggers are left alone. If using Python 2.7/Python 3.2 or later, you may wish to consider the dictConfig() API which is better than fileConfig() as it gives more control over the configuration.

这篇关于在多个模块中使用日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆