bronx.fancies.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
Module Attributes¶
- bronx.fancies.loggers.roots = {'bronx'}
The actual set of pseudo-root loggers created
- bronx.fancies.loggers.predefined_formats = {'default': <logging.Formatter object>, 'fixsize': <logging.Formatter object>}
Default formatters
- bronx.fancies.loggers.default_console = <StreamHandler <stderr> (DEBUG)>
Console handler
Functions¶
- bronx.fancies.loggers.contextboundGlobalLevel(level)[source]¶
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.
- bronx.fancies.loggers.contextboundRedirectStdout(outputs=None)[source]¶
Within this context manager, redirect all the outputs to a StringIO object.
- bronx.fancies.loggers.fdecoGlobalLevel(level)[source]¶
Function decorator that set loglevels to
level.When the function exits, loglevels are restored to their previous values.
- bronx.fancies.loggers.getActualLevel(level)[source]¶
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.
- bronx.fancies.loggers.getLogger(modname)[source]¶
Return a standard logger in the scope of an appropriate root logger.
- bronx.fancies.loggers.setGlobalLevel(level)[source]¶
Explicitly sets the logging level to the
levelvalue for everyone.
- bronx.fancies.loggers.setLogMethods(logger, methods=('debug', 'info', 'warning', 'error', 'critical'))[source]¶
Reset some loggers methods with methods from an external logger.
- Note:
This method is realy disturbing. Its use won’t be allowed anymore with Python3
Classes¶
- class bronx.fancies.loggers.PromptAwareLoggingFilter(name='')[source]¶
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.
- class bronx.fancies.loggers.SlurpHandler(records_stack)[source]¶
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.