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 the extra 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.

class logbook.more.TaggingHandler(handlers, filter=None, bubble=False)[source]

A handler that logs for tags and dispatches based on those.

Example:

import logbook
from logbook.more import TaggingHandler

handler = TaggingHandler(dict(
    info=OneHandler(),
    warning=AnotherHandler()
))

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

get_oauth_token()[source]

Returns the oauth access token.

make_client()[source]

Creates a new oauth client auth a new access token.

tweet(status)[source]

Tweets a given status. Status must not exceed 140 chars.

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 the logbook.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.

class logbook.more.ColorizingStreamHandlerMixin[source]

A mixin class that does colorizing.

New in version 0.3.

get_color(record)[source]

Returns the color for this record.

should_colorize(record)[source]

Returns True if colorizing should be applied to this record. The default implementation returns True if the stream is a tty and we are not executing on windows.

Other

class logbook.more.JinjaFormatter(template)[source]

A formatter object that makes it easy to format using a Jinja 2 template instead of a format string.