The More Module¶
The more module implements special handlers and other things that are
beyond the scope of Logbook itself or depend on external libraries.
Additionally there are some handlers in logbook.ticketing
,
logbook.queues
and logbook.notifiers
.
Tagged Logging¶
-
class
logbook.more.
TaggingLogger
(name=None, tags=None)[source]¶ A logger that attaches a tag to each record. This is an alternative record dispatcher that does not use levels but tags to keep log records apart. It is constructed with a descriptive name and at least one tag. The tags are up for you to define:
logger = TaggingLogger('My Logger', ['info', 'warning'])
For each tag defined that way, a method appears on the logger with that name:
logger.info('This is a info message')
To dispatch to different handlers based on tags you can use the
TaggingHandler
.The tags themselves are stored as list named
'tags'
in theextra
dictionary.-
call_handlers
(record)¶ Pass a record to all relevant handlers in the following order:
- per-dispatcher handlers are handled first
- afterwards all the current context handlers in the order they were pushed
Before the first handler is invoked, the record is processed (
process_record()
).
-
handle
(record)¶ Call the handlers for the specified record. This is invoked automatically when a record should be handled. The default implementation checks if the dispatcher is disabled and if the record level is greater than the level of the record dispatcher. In that case it will call the handlers (
call_handlers()
).
-
make_record_and_handle
(level, msg, args, kwargs, exc_info, extra)¶ Creates a record from some given arguments and heads it over to the handling system.
-
process_record
(record)¶ Processes the record with all context specific processors. This can be overriden to also inject additional information as necessary that can be provided by this record dispatcher.
-
Special Handlers¶
-
class
logbook.more.
TwitterHandler
(consumer_key, consumer_secret, username, password, level=0, format_string=None, filter=None, bubble=False)[source]¶ A handler that logs to twitter. Requires that you sign up an application on twitter and request xauth support. Furthermore the oauth2 library has to be installed.
If you don’t want to register your own application and request xauth credentials, there are a couple of leaked consumer key and secret pairs from application explicitly whitelisted at Twitter (leaked secrets).
-
formatter_class
¶ alias of
TwitterFormatter
-
-
class
logbook.more.
ExternalApplicationHandler
(arguments, stdin_format=None, encoding='utf-8', level=0, filter=None, bubble=False)[source]¶ This handler invokes an external application to send parts of the log record to. The constructor takes a list of arguments that are passed to another application where each of the arguments is a format string, and optionally a format string for data that is passed to stdin.
For example it can be used to invoke the
say
command on OS X:from logbook.more import ExternalApplicationHandler say_handler = ExternalApplicationHandler(['say', '{record.message}'])
Note that the above example is blocking until
say
finished, so it’s recommended to combine this handler with thelogbook.ThreadedWrapperHandler
to move the execution into a background thread.New in version 0.3.
-
class
logbook.more.
ExceptionHandler
(exc_type, level=0, format_string=None, filter=None, bubble=False)[source]¶ An exception handler which raises exceptions of the given exc_type. This is especially useful if you set a specific error level e.g. to treat warnings as exceptions:
from logbook.more import ExceptionHandler class ApplicationWarning(Exception): pass exc_handler = ExceptionHandler(ApplicationWarning, level='WARNING')
New in version 0.3.
Colorized Handlers¶
New in version 0.3.
-
class
logbook.more.
ColorizedStderrHandler
(level=0, format_string=None, filter=None, bubble=False)[source]¶ A colorizing stream handler that writes to stderr. It will only colorize if a terminal was detected. Note that this handler does not colorize on Windows systems.
New in version 0.3.