This documents compatibility support with existing systems such as logging and warnings.

Logging Compatibility


Permanently redirects logging to the stdlib. This also removes all otherwise registered handlers on root logger of the logging system but leaves the other loggers untouched.

Parameters:set_root_logger_level – controls of the default level of the legacy root logger is changed so that all legacy log messages get redirected to Logbook

Temporarily redirects logging for all threads and reverts it later to the old handlers. Mainly used by the internal unittests:

from logbook.compat import redirected_logging
with redirected_logging():
class logbook.compat.RedirectLoggingHandler

A handler for the stdlib’s logging system that redirects transparently to logbook. This is used by the redirect_logging() and redirected_logging() functions.

If you want to customize the redirecting you can subclass it.


Converts a logging level into a logbook level.


Converts an old logging record into a logbook log record.


Converts the UNIX timestamp of the old record into a datetime object as used by logbook.


Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.


Tries to find the caller that issued the call.


Tries to find custom data from the old logging record. The return value is a dictionary that is merged with the log record extra dictionaries.

class logbook.compat.LoggingHandler(logger=None, level=0, filter=None, bubble=False)

Does the opposite of the RedirectLoggingHandler, it sends messages from logbook to logging. Because of that, it’s a very bad idea to configure both.

This handler is for logbook and will pass stuff over to a logger from the standard library.

Example usage:

from logbook.compat import LoggingHandler, warn
with LoggingHandler():
    warn('This goes to logging')

Converts a logbook level into a logging level.


Converts a record from logbook to logging.


Converts a datetime object into a timestamp.


Emit the specified logging record. This should take the record and deliver it to whereever the handler sends formatted log records.


Returns the logger to use for this record. This implementation always return logger.

Warnings Compatibility


Like redirected_warnings() but will redirect all warnings to the shutdown of the interpreter:

from logbook.compat import redirect_warnings

A context manager that copies and restores the warnings filter upon exiting the context, and logs warnings using the logbook system.

The channel attribute of the log record will be the import name of the warning.

Example usage:

from logbook.compat import redirected_warnings
from warnings import warn

with redirected_warnings():
    warn(DeprecationWarning('logging should be deprecated'))