日志拦截

(类来自 pyomo.common.log)

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

基础类:object

用于拦截发送到日志流的消息的上下文管理器

这个类旨在简化日志消息的测试。

LoggingIntercept 上下文管理器将拦截发送到与指定级别匹配的日志流的消息,并将这些消息发送到指定的输出流。注册到目标日志记录器的其他处理程序将被暂时移除,并且日志记录器将被设置为不将消息传播到更高级别的日志记录器。

Parameters:
  • 输出 (io.TextIOBase) – 用于发送日志消息的文件流

  • 模块 (str) – 要拦截的目标记录器名称

  • level (int) – 要拦截的日志记录级别

  • formatter (logging.Formatter) – 用于渲染日志消息的格式化器。如果未指定,则使用 ‘%(message)s’

示例

>>> 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]

方法

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

成员文档