sphinx/doc/extdev/logging.rst

78 lines
2.7 KiB
ReStructuredText
Raw Normal View History

2016-12-28 06:14:56 -06:00
.. _logging-api:
Logging API
===========
.. function:: sphinx.util.logging.getLogger(name)
2017-02-07 13:03:07 -06:00
Returns a logger wrapped by :class:`SphinxLoggerAdapter` with the specified *name*.
2016-12-28 06:14:56 -06:00
Example usage::
2017-02-07 13:03:07 -06:00
from sphinx.util import logging # Load on top of python's logging module
2016-12-28 06:14:56 -06:00
logger = logging.getLogger(__name__)
logger.info('Hello, this is an extension!')
.. class:: SphinxLoggerAdapter(logging.LoggerAdapter)
.. method:: SphinxLoggerAdapter.error(level, msg, *args, **kwargs)
.. method:: SphinxLoggerAdapter.critical(level, msg, *args, **kwargs)
.. method:: SphinxLoggerAdapter.warning(level, msg, *args, **kwargs)
2017-02-07 13:03:07 -06:00
Logs a message on this logger with the specified level.
Basically, the arguments are as with python's logging module.
2016-12-28 06:14:56 -06:00
In addition, Sphinx logger supports following keyword arguments:
**type**, ***subtype***
2017-02-07 13:03:07 -06:00
Categories of warning logs. It is used to suppress
2016-12-28 06:14:56 -06:00
warnings by :confval:`suppress_warnings` setting.
**location**
2017-02-07 13:03:07 -06:00
Where the warning happened. It is used to include
the path and line number in each log. It allows docname,
2016-12-28 06:14:56 -06:00
tuple of docname and line number and nodes::
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**
2017-02-07 13:03:07 -06:00
The color of logs. By default, warning level logs are
2016-12-28 06:14:56 -06:00
colored as ``"darkred"``. The others are not colored.
.. method:: SphinxLoggerAdapter.log(level, msg, *args, **kwargs)
.. method:: SphinxLoggerAdapter.info(level, msg, *args, **kwargs)
.. method:: SphinxLoggerAdapter.verbose(level, msg, *args, **kwargs)
.. method:: SphinxLoggerAdapter.debug(level, msg, *args, **kwargs)
2017-02-07 13:03:07 -06:00
Logs a message to this logger with the specified level.
Basically, the arguments are as with python's logging module.
2016-12-28 06:14:56 -06:00
In addition, Sphinx logger supports following keyword arguments:
**nonl**
2017-02-07 13:03:07 -06:00
If true, the logger does not fold lines at the end of the log message.
2016-12-28 06:14:56 -06:00
The default is ``False``.
**color**
2017-02-07 13:03:07 -06:00
The color of logs. By default, debug level logs are
colored as ``"darkgray"``, and debug2 level ones are ``"lightgray"``.
2016-12-28 06:14:56 -06:00
The others are not colored.
.. function:: pending_logging()
2017-02-07 13:03:07 -06:00
Marks all logs as pending::
2016-12-28 06:14:56 -06:00
with pending_logging():
logger.warning('Warning message!') # not flushed yet
some_long_process()
# the warning is flushed here
.. function:: pending_warnings()
2017-02-07 13:03:07 -06:00
Marks warning logs as pending. Similar to :func:`pending_logging`.