I am currently developing a package which can be used without writing any new code and the modules can be used to develop new code (see documentation).
Many of my modules use logging
in a very simple way:
import logging import sys logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG, stream=sys.stdout) # Code of the module follows
I have these lines in many modules. It seems to me that I should factor it out, but I am not sure what the best / recommended / most pythonic way to do so is.
The relevant parts in the logging documentation seem to be Configuring Logging for a Library and Logging from multiple modules.
I guess I should simply move the line logging.basicConfig
to the executables (bin/hwrt
) and remove all other logging.basicConfig
lines.
Is there any rule how packages should use logging (like PEP8 for coding style)?
If other developers use my code they might want to disable / modify the way my package does logging (so that it doesn't get mixed with their logging calls). Is there a way to help them do so?
Instead, just set up your logger object based on __name__ , use that, and that's it. Document that you use logging , and developers using your library can use the standard logging API to configure logging. All this is already documented in the logging module; you can consider it the style guide for Python logging.
Python comes with a logging module in the standard library that provides a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is the first go-to point for most developers when it comes to logging.
In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.
Your library should not configure logging; that's an application-wide task.
Instead, just set up your logger object based on __name__
, use that, and that's it. Document that you use logging
, and developers using your library can use the standard logging
API to configure logging.
You could add a null handler to your root logger (the logger registered for your package name) to prevent a default configuration being used if the application didn't set one:
# This goes into your library somewhere logging.getLogger('name.of.library').addHandler(logging.NullHandler())
and developers using your library can then disable all logging just for your library by disabling log propagation:
logging.getLogger('name.of.library').propagate = False
All this is already documented in the logging
module; you can consider it the style guide for Python logging. From the Configuring Logging for a Library section you already linked to:
Note: It is strongly advised that you do not add any handlers other than NullHandler to your library’s loggers. This is because the configuration of handlers is the prerogative of the application developer who uses your library. 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.
logging.basicConfig()
does just that; it creates handler configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With