简体   繁体   English

库记录器是否应该只有空处理程序?

[英]Should library loggers have only null handlers?

I am developing a new library and I am completely new to the concept of logging. 我正在开发一个新的库,并且对日志记录的概念完全陌生。

I have added logging using Python's logging module for same. 我已经使用Python的日志记录模块添加了日志记录。 The logging I gave has a specific FileHandler set for debug level and StreamHandler set at Warning level. 我提供的日志记录将特定的FileHandler设置为调试级别,将StreamHandler设置为警告级别。 Python documentation about logging says libraries should have only Null Handlers. 有关日志记录的Python文档说,库应仅具有Null处理程序。 Here is the documentation link https://docs.python.org/3/howto/logging.html#library-config 这是文档链接https://docs.python.org/3/howto/logging.html#library-config

Will it be a problem if I still have an exclusive file and stream handlers in my library. 如果我的库中仍然有独占文件和流处理程序,是否会有问题?

I am not able to understand why one should create logs in libraries if they cannot have their own customized handlers. 我不明白为什么如果他们不能拥有自己的自定义处理程序,为什么应该在库中创建日志。

It would be very helpful if someone could clear my understanding gap about implementing logging in libraries. 如果有人可以消除我对在库中实现日志记录的理解空白,那将非常有帮助。

A secondary question: How will an application developer who uses my library be able to access/enable the logs that I created in the library if I set Null handler? 另一个问题:如果设置了Null处理程序,那么使用我的库的应用程序开发人员将如何访问/启用我在库中创建的日志?

to your first question - [from the python docs] - 您的第一个问题-[来自python文档]-

"The application developer knows their target audience and what handlers are most appropriate for their application: if you add handlers 'under the hood', you might well interfere with their ability to carry out unit tests and deliver logs which suit their requirements." “应用程序开发人员知道他们的目标受众以及最适合他们的应用程序的处理程序:如果在幕后添加处理程序,则很可能会干扰他们进行单元测试和交付适合其要求的日志的能力。” 1 1

as a user of your library, I may want to show logs from your_pkg.foo.baz, but not your_pkg.foo module. 作为您的库的用户,我可能希望显示来自your_pkg.foo.baz的日志,而不是来自your_pkg.foo模块的日志。 adding handlers from within your library may force me to do that (depending on the log level that was set to the loggers and handlers). 从您的库中添加处理程序可能会迫使我这样做(取决于设置为记录器和处理程序的日志级别)。

to your second question - adding a Nullhandler allows a user to choose his custom logging needs by configuring new handlers through the logging.get_logger("your_pkg.foo.baz").add_handler(...). 第二个问题-添加Nullhandler允许用户通过logging.get_logger(“ your_pkg.foo.baz”)。add_handler(...)配置新的处理程序,从而选择自己的自定义日志记录需求。

to fully understand the logging mechanism (loggers, handlers, filters, and propagation)- you could look here - logging flow 要完全了解日志记录机制(日志记录器,处理程序,过滤器和传播),您可以在此处查看- 日志记录流

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

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