简体   繁体   English

重新定义日志记录根记录器

[英]Redefining logging root logger

At my current project there are thousand of code lines which looks like this: 在我目前的项目中,有数千个代码行,如下所示:

logging.info("bla-bla-bla")

I don't want to change all these lines, but I would change log behavior. 我不想改变所有这些行,但我会改变日志行为。 My idea is changing root logger to other Experimental logger, which is configured by ini-file: 我的想法是将根记录器更改为其他Experimental记录器,它由ini-file配置:

[loggers]
keys =  Experimental

[formatter_detailed]
format = %(asctime)s:%(name)s:%(levelname)s %(module)s:%(lineno)d:  %(message)s

[handler_logfile]
class = FileHandler
args = ('experimental.log', 'a')
formatter = detailed

[logger_Experimental]
level = DEBUG
qualname = Experimental
handlers = logfile
propagate = 0

Now setting the new root logger is done by this piece of code: 现在设置新的根记录器是由这段代码完成的:

logging.config.fileConfig(path_to_logger_config)
logging.root = logging.getLogger('Experimental')

Is redefining of root logger safe? 重新定义根记录器是否安全? Maybe there is more convenient way? 也许有更方便的方法?

I've tried to use google and looked through stackoverflow questions, but I didn't find the answer. 我试图使用谷歌并查看stackoverflow问题,但我没有找到答案。

You're advised not to redefine the root logger in the way you describe. 建议您不要以您描述的方式重新定义根记录器。 In general you should only use the root logger directly for small scripts - for larger applications, best practice is to use 通常,您只应将根记录器直接用于小脚本 - 对于较大的应用程序,最佳做法是使用

logger = logging.getLogger(__name__)

in each module where you use logging, and then make calls to logger.info() etc. 在每个使用日志记录的模块中,然后调用logger.info()等。

If all you want to do is to log to a file, why not just add a file handler to the root logger? 如果您只想记录到文件,为什么不直接向根记录器添加文件处理程序? You can do using eg 你可以用eg

if __name__ == '__main__':
    logging.basicConfig(filename='experimental.log', filemode='w')
    main() # or whatever your main entry point is called

or via a configuration file. 或通过配置文件。

Update: When I say "you're advised", I mean by me, here in this answer ;-) While you may not run into any problems in your scenario, it's not good practice to overwrite a module attribute which hasn't been designed to be overwritten. 更新:当我说“你被告知”时,我的意思是我,在这个答案中;-)虽然你可能没有在你的场景中遇到任何问题,但是覆盖一个尚未出现的模块属性并不是一个好习惯。旨在被覆盖。 For example, the root logger is an instance of a different class (which is not part of the public API), and there are other references to it in the logging machinery which would still point to the old value. 例如,根记录器是不同类的实例(不是公共API的一部分),并且在日志记录机器中还有其他对它的引用,它仍将指向旧值。 Either of these facts could lead to hard-to-debug problems. 这些事实中的任何一个都可能导致难以调试的问题。 Since the logging package allows a number of ways of achieving what you seem to want (seemingly, logging to a file rather than the console), then you should use those mechanisms that have been provided. 由于日志包允许多种方式来实现您想要的内容(貌似,记录到文件而不是控制台),因此您应该使用已提供的那些机制。

logger = logging.getLogger() Leaving the name empty will return you the root logger. logger = logging.getLogger()保留名称为空将返回根记录器。

logger = logging.getLogger('name') Gives you another logger. logger = logging.getLogger('name')给你另一个记录器。

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

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