如何在应用程序中配置所有记录器 [英] How to configure all loggers in an application
问题描述
Python的日志记录模块允许模块或类定义自己的日志记录器.并且不同的记录器可以具有不同的处理程序.他们中的一些人可能选择登录到文件,而另一些人选择登录到例如stdout.
现在我的应用程序使用了其中的几个模块,每个模块都有自己的记录器,这些记录器具有各种处理程序.我可以统一日志记录行为,以便所有日志都进入我指定的日志文件吗?换句话说,有没有一种方法可以一次性从单个位置.config()所有记录器的处理程序?
您可能应该查看 Python Logging HOWTO 了解其工作原理.
简而言之,所有模块通常要做的就是获取格式为G_LOG = logging.getLogger('package.name')
的记录器,并将消息发送到记录器:G_LOG.info('some message'), G_LOG.exception('something bad happened')
.模块通常不应配置任何内容.
使用模块的应用程序可以基于记录器名称打开日志并配置处理程序:
- 收听所有消息,或
- 仅收听超过特定阈值的邮件,或者
- 仅侦听名称以
package
开头的记录器的消息,或 - 仅监听名称以
package.name
开头的记录器的消息,等等
最简单的方法是通过某个地方的 logging.basicConfig 配置日志记录在应用程序的开头:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=log_file, filemode='a')
这样,您会将所有模块的所有日志记录消息写入log_file
.
如果您需要更详细的日志记录策略(将日志从不同的记录器放到不同的文件中,或将堆栈跟踪发送到单独的文件中),最好定义一个日志记录配置文件并使用 解决方案
You should probably look into the Python Logging HOWTO to understand how it works.
In short, all that modules usually do is getting a logger of the form G_LOG = logging.getLogger('package.name')
and sending messages to the logger: G_LOG.info('some message'), G_LOG.exception('something bad happened')
. Modules should not usually configure anything.
The application that uses the modules can turn the logging on and configure the handlers based on the logger names:
- listen all messages, or
- listen only messages above a certain threshold, or
- listen messages only from loggers whose name starts with
package
, or - listen messages only from loggers whose name starts woth
package.name
, etc
The easiest way is to configure logging through logging.basicConfig somewhere in the beginning of your application:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=log_file, filemode='a')
That way you will write all logging messages from all modules to the log_file
.
If you need a more detailed logging strategy (put logs from different loggers to different files, or send stacktraces to a separate file), it is better to define a logging config file and configure logging using logging.config.dictConfig or logging.config.fileConfig
.
P.S. I usually create two loggers as module variables:
G_LOG = logging.getLogger(__name__)
ST_LOG = logging.getLogger('stacktrace.' + __name__)
to G_LOG
I send only one-line messages. To ST_LOG
I duplicate important messages using ST_LOG.exception
which implicitly has exc_info=True
and writes the stacktrace of the current exception.
At the start of the application I load a configuration that configures two loggers (and two file handlers for them): one that receives messages that start with stacktrace
and has propagate=0
(that is stacktrace messages are not visible at the top) and the root logger that handles the rest of the messages. I will not put here my complete log config files, since it is a useful home work to understand how it all works.
这篇关于如何在应用程序中配置所有记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!