简体   繁体   English

python logger的后代-好处

[英]python logger's descendants - benefits

I'm having trouble to understand the benefits of creating descendants loggers out of my root logger, in each module. 我很难理解在每个模块中从根记录器中创建后代记录器的好处。 For instance: 例如:

In project called "foo", module name called "bar": 在名为“ foo”的项目中,模块名称为“ bar”:

import logging
logger = logging.getLogger(__name__)
# other pieces of code, that all of them use log...

And same project, but in module "baz" 和相同的项目,但在模块“ baz”中

import logging
logger = logging.getLogger(__name__)
# other pieces of code, that all of them use log...

instead of just, creating one logger object, and make other modules import and use it. 而不只是创建一个记录器对象,然后导入和使用其他模块。 For instance in same project: 例如在同一项目中:

from foo.logs import logger
# other pieces of code, that all of them use log...

and foo/logs.py would contain something like: 和foo / logs.py将包含如下内容:

import logging
logger = logging.getLoger('foo')
# some init logic of the logger...

I can get all data I need from the created LogRecord object: filename, funcName, pathname, name, etc... 我可以从创建的LogRecord对象获取所需的所有数据:文件名,funcName,路径名,名称等。

Why repeat same procedure of creating a descendant logger (calling getLogger) when I use one? 为什么使用一个时重复创建后代记录器的相同过程(调用getLogger)? it may also have a minor performance footprint no? 它可能也有较小的性能足迹吗?

The reason for creating a separate logger for each file is so that you can later customize what is and what isn't logged without changing the source code. 为每个文件创建单独的记录器的原因是,以便您以后可以自定义记录的内容和不记录的内容,而无需更改源代码。

Logging is usually configured via an external file, which can be modified after the installation is installed, even by users who are not software developers. 日志记录通常通过外部文件进行配置,安装后,即使不是软件开发人员的用户也可以对其进行修改。

For the same reason, there are different logging levels which can be used with each logger, in order to allow more or less logs to be produced. 出于相同的原因,每个记录器可以使用不同的记录级别,以允许生成更多或更少的日志。 Then at some point, when eg exhaustive log data from module a is needed for debugging purposes, the configuration file can be modified to show DEBUG log level for module a without cluttering up the log file with debug info from every module in the application. 然后,在某个时候,例如,出于调试目的而需要来自模块a详尽日志数据时,可以修改配置文件以显示模块a DEBUG日志级别,而不会弄乱应用程序中每个模块的调试信息。

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

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