footprints.loggers¶
This module provides a few functions on top of the standard logging module in order to easily create new loggers (including root ones) and control their verbosity level.
The interface to this module is made of two functions: getLogger() and
setGlobalLevel().
Example:
# Create a main logger (a :class:`logging.Logger` object is returned)
>>> totolog = getLogger('toto')
# From now and on, the root logger 'toto' is created and properly initialised
# (i.e. a console handler is added to it and a :class:`LoggingFilter` instance
# is set as filter.
# its name has been recording in the 'roots' module level variable
# as well as in the 'lognames' module level variable
>>> 'toto' in roots
True
>>> 'toto' in lognames
True
# It can be fetched a second time (for example in another module): the same
# object will be returned
>>> totolog2 = getLogger('toto')
>>> totolog2 is totolog
True
>>> totolog.debug('By default, only info messages are shown')
>>> totolog.info('This will show up !')
# The logging level for all root loggers can easily be changed (at once)
>>> newlevel = setGlobalLevel('warning')
>>> newlevel == logging.WARNING
True
# One can still set the verbosity level per logger
>>> getLogger('toto').setLevel(logging.ERROR)
# Sub-loggers can easily be created
>>> totosublog = getLogger('toto.sub')
# its name has been recording in the 'lognames' module level variable
>>> 'toto.sub' in lognames
True
Functions¶
- footprints.loggers.contextboundGlobalLevel(level)¶
Within this context manager, explicitly sets the logging level to the
levelvalue for everyone.When the context exists, logging levels are restored to their previous values.
- footprints.loggers.contextboundRedirectStdout(outputs=None)¶
Within this context manager, redirect all the outputs to a StringIO object.
- footprints.loggers.fdecoGlobalLevel(level)¶
Function decorator that set loglevels to
level.When the function exits, loglevels are restored to their previous values.
- footprints.loggers.getActualLevel(level)¶
Return the actual level value as long as the argument is valid.
levelcan be a verbosity level name (e.g debug, info, …) or the number associated with it.
- footprints.loggers.getLogger(modname)¶
Return a standard logger in the scope of an appropriate root logger.
- footprints.loggers.setGlobalLevel(level)¶
Explicitly sets the logging level to the
levelvalue for everyone.
- footprints.loggers.setRootLogger(logger)¶
Set appropriate Handler and Console t a top level logger.
- footprints.loggers.unittestGlobalLevel(level)¶
Function decorator that set loglevels to
levelduring unit tests execution.
Classes¶
- class footprints.loggers.PromptAwareLoggingFilter(name='')¶
Bases:
FilterAdd module name to record.
Initialize a filter.
Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.
- filter(record)¶
Remap top interactive module to
prompt.
- class footprints.loggers.SlurpHandler(records_stack)¶
Bases:
HandlerA strange Handler that accumulates the log-records in a list.
We try to make sure that each individual record is pickable.
Initializes the instance - basically setting the formatter to None and the filter list to empty.
- emit(record)¶
Emit a record.
Adds the LogRecord to the stack, preparing it for pickling first.
- Parameters:
record – The record to emit.
- prepare(record)¶
Prepares a record for queuing.
The base implementation formats the record to merge the message and arguments, and removes unpickleable items from the record in-place.
- Parameters:
record – The record to prepare.