LoggingIntercept

(class from pyomo.common.log)

class pyomo.common.log.LoggingIntercept(output=None, module=None, level=30, formatter=None)[source]

Bases: object

Context manager for intercepting messages sent to a log stream

This class is designed to enable easy testing of log messages.

The LoggingIntercept context manager will intercept messages sent to a log stream matching a specified level and send the messages to the specified output stream. Other handlers registered to the target logger will be temporarily removed and the logger will be set not to propagate messages up to higher-level loggers.

Parameters:
  • output (io.TextIOBase) – the file stream to send log messages to

  • module (str) – the target logger name to intercept

  • level (int) – the logging level to intercept

  • formatter (logging.Formatter) – the formatter to use when rendering the log messages. If not specified, uses ‘%(message)s’

Examples

>>> import io, logging
>>> from pyomo.common.log import LoggingIntercept
>>> buf = io.StringIO()
>>> with LoggingIntercept(buf, 'pyomo.core', logging.WARNING):
...     logging.getLogger('pyomo.core').warning('a simple message')
>>> buf.getvalue()
'a simple message\n'
__init__(output=None, module=None, level=30, formatter=None)[source]

Methods

__init__([output, module, level, formatter])

Member Documentation