Compatibility¶
This documents compatibility support with existing systems such as
logging
and warnings
.
Logging Compatibility¶
- logbook.compat.redirect_logging(set_root_logger_level=True)[source]¶
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
- logbook.compat.redirected_logging(set_root_logger_level=True)[source]¶
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[source]¶
A handler for the stdlib’s logging system that redirects transparently to logbook. This is used by the
redirect_logging()
andredirected_logging()
functions.If you want to customize the redirecting you can subclass it.
- convert_time(timestamp)[source]¶
Converts the UNIX timestamp of the old record into a datetime object as used by logbook.
- class logbook.compat.LoggingHandler(logger=None, level=0, filter=None, bubble=False)[source]¶
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')
Warnings Compatibility¶
- logbook.compat.redirect_warnings()[source]¶
Like
redirected_warnings()
but will redirect all warnings to the shutdown of the interpreter:from logbook.compat import redirect_warnings redirect_warnings()
- logbook.compat.redirected_warnings()[source]¶
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'))