Logging becomes more useful the higher the number of components in a system that are using it. Logbook itself is not a widely supported library so far, but a handful of libraries are using the logging already which can be redirected to Logbook if necessary.
Logbook itself is easier to support for libraries than logging because it does away with the central logger registry and can easily be mocked in case the library is not available.
If you want to support Logbook in your library but not depend on it you can copy/paste the following piece of code. It will attempt to import logbook and create a Logger and if it fails provide a class that just swallows all calls:
try:
from logbook import Logger
except ImportError:
class Logger(object):
def __init__(self, name, level=0):
self.name = name
self.level = level
debug = info = warn = warning = notice = error = exception = \
critical = log = lambda *a, **kw: None
log = Logger('My library')
Sometimes you want to have loggers in place that are only really good for debugging. For example you might have a library that does a lot of server/client communication and for debugging purposes it would be nice if you can enable/disable that log output as necessary.
In that case it makes sense to create a logger and disable that by default and give people a way to get hold of the logger to flip the flag. Additionally you can override the disabled flag to automatically set it based on another value:
class MyLogger(Logger):
@property
def disabled(self):
return not database_connection.debug
database_connection.logger = MyLogger('mylibrary.dbconnection')