日志接口

sphinx.util.logging.getLogger(name)[源代码]

获取由 sphinx.util.logging.SphinxLoggerAdapter 类包装的logger。

Sphinx logger始终使用 Sphinx.* 命名空间独立于根记录器的设置。它确保即使第三方扩展或导入的应用程序重置记录器设置,日志记录也是一致的。

用法示例:

>>> from sphinx.util import logging
>>> logger = logging.getLogger(__name__)
>>> logger.info('Hello, this is an extension!')
Hello, this is an extension!
class sphinx.util.logging.SphinxLoggerAdapter(logging.LoggerAdapter)[源代码]

LoggerAdapter允许使用 typesubtype 关键字。

error(msg, *args, **kwargs)
critical(msg, *args, **kwargs)
warning(msg, *args, **kwargs)[源代码]

在此记录器上记录具有指定级别的消息。基本上,参数与python的日志模块一样。

此外,Sphinx logger支持以下关键字参数:

type, subtype

警告日志的类别。它用于通过以下方式抑制警告 suppress_warnings 设置。

location

警告发生的地方。它用于在每个日志中包括路径和行号。它允许docname、docname元组、行号和节点:

logger = sphinx.util.logging.getLogger(__name__)
logger.warning('Warning happened!', location='index')
logger.warning('Warning happened!', location=('chapter1/index', 10))
logger.warning('Warning happened!', location=some_node)
color

日志的颜色。默认情况下,错误级别的日志被标记为 "darkred",严重级别的日志不被着色,警告级别的日志被标记为 "red"

log(level, msg, *args, **kwargs)[源代码]
info(msg, *args, **kwargs)
verbose(msg, *args, **kwargs)[源代码]
debug(msg, *args, **kwargs)

使用指定级别将消息记录到此记录器。基本上,参数与python的日志模块一样。

此外,Sphinx logger支持以下关键字参数:

nonl

如果为true,则记录器不会在日志消息末尾折叠行。默认值为 False

location

消息发出的地方。有关详细信息,请参见 SphinxLoggerAdapter.warning()

color

日志的颜色。默认情况下,信息和详细级别的日志不着色,而调试级别的日志被着色为 "darkgray"

sphinx.util.logging.pending_logging()[源代码]

上下文管理器,用于暂时推迟记录所有日志。

例如:

>>> with pending_logging():
>>>     logger.warning('Warning message!')  # not flushed yet
>>>     some_long_process()
>>>
Warning message!  # the warning is flushed here
sphinx.util.logging.pending_warnings()[源代码]

上下文管理器,用于暂时推迟记录警告。

类似于 pending_logging()

sphinx.util.logging.prefixed_warnings()[源代码]

上下文管理器,用于临时将前缀添加到所有警告日志记录中。

例如:

>>> with prefixed_warnings("prefix:"):
>>>     logger.warning('Warning message!')  # => prefix: Warning message!

在 2.0 版本加入.