-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.py
More file actions
223 lines (172 loc) · 7.68 KB
/
logging.py
File metadata and controls
223 lines (172 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Logging utility functions for Sphinx."""
from __future__ import annotations
import logging
import logging.handlers
from collections import defaultdict
from contextlib import contextmanager, nullcontext
from typing import IO, TYPE_CHECKING, Any
from docutils import nodes
from docutils.utils import get_source_line
from sphinx.errors import SphinxWarning
from sphinx.util.console import colorize
from sphinx.util.osutil import abspath
if TYPE_CHECKING:
from collections.abc import Iterator, Sequence, Set
from typing import NoReturn
from docutils.nodes import Node
from sphinx.application import Sphinx
NAMESPACE = 'sphinx'
VERBOSE = 15
LEVEL_NAMES: defaultdict[str, int] = defaultdict(lambda: logging.WARNING, {'CRITICAL': logging.CRITICAL, 'SEVERE': logging.CRITICAL, 'ERROR': logging.ERROR, 'WARNING': logging.WARNING, 'INFO': logging.INFO, 'VERBOSE': VERBOSE, 'DEBUG': logging.DEBUG})
VERBOSITY_MAP: defaultdict[int, int] = defaultdict(lambda: logging.NOTSET, {0: logging.INFO, 1: VERBOSE, 2: logging.DEBUG})
COLOR_MAP: defaultdict[int, str] = defaultdict(lambda: 'blue', {logging.ERROR: 'darkred', logging.WARNING: 'red', logging.DEBUG: 'darkgray'})
def getLogger(name: str) -> SphinxLoggerAdapter:
"""Get logger wrapped by :class:`sphinx.util.logging.SphinxLoggerAdapter`.
Sphinx logger always uses ``sphinx.*`` namespace to be independent from
settings of root logger. It ensures logging is consistent even if a
third-party extension or imported application resets logger settings.
Example usage::
>>> from sphinx.util import logging
>>> logger = logging.getLogger(__name__)
>>> logger.info('Hello, this is an extension!')
Hello, this is an extension!
"""
pass
def convert_serializable(records: list[logging.LogRecord]) -> None:
"""Convert LogRecord serializable."""
pass
class SphinxLogRecord(logging.LogRecord):
"""Log record class supporting location"""
prefix = ''
location: Any = None
class SphinxInfoLogRecord(SphinxLogRecord):
"""Info log record class supporting location"""
prefix = ''
class SphinxWarningLogRecord(SphinxLogRecord):
"""Warning log record class supporting location"""
class SphinxLoggerAdapter(logging.LoggerAdapter):
"""LoggerAdapter allowing ``type`` and ``subtype`` keywords."""
KEYWORDS = ['type', 'subtype', 'location', 'nonl', 'color', 'once']
def warning(self, msg: object, *args: object, type: None | str=None, subtype: None | str=None, location: None | str | tuple[str | None, int | None] | Node=None, nonl: bool=True, color: str | None=None, once: bool=False, **kwargs: Any) -> None:
"""Log a sphinx warning.
It is recommended to include a ``type`` and ``subtype`` for warnings as
these can be displayed to the user using :confval:`show_warning_types`
and used in :confval:`suppress_warnings` to suppress specific warnings.
It is also recommended to specify a ``location`` whenever possible
to help users in correcting the warning.
:param msg: The message, which may contain placeholders for ``args``.
:param args: The arguments to substitute into ``msg``.
:param type: The type of the warning.
:param subtype: The subtype of the warning.
:param location: The source location of the warning's origin,
which can be a string (the ``docname`` or ``docname:lineno``),
a tuple of ``(docname, lineno)``,
or the docutils node object.
:param nonl: Whether to append a new line terminator to the message.
:param color: A color code for the message.
:param once: Do not log this warning,
if a previous warning already has same ``msg``, ``args`` and ``once=True``.
"""
pass
class WarningStreamHandler(logging.StreamHandler):
"""StreamHandler for warnings."""
pass
class NewLineStreamHandler(logging.StreamHandler):
"""StreamHandler which switches line terminator by record.nonl flag."""
class MemoryHandler(logging.handlers.BufferingHandler):
"""Handler buffering all logs."""
buffer: list[logging.LogRecord]
def __init__(self) -> None:
super().__init__(-1)
@contextmanager
def pending_warnings() -> Iterator[logging.Handler]:
"""Context manager to postpone logging warnings temporarily.
Similar to :func:`pending_logging`.
"""
pass
@contextmanager
def suppress_logging() -> Iterator[MemoryHandler]:
"""Context manager to suppress logging all logs temporarily.
For example::
>>> with suppress_logging():
>>> logger.warning('Warning message!') # suppressed
>>> some_long_process()
>>>
"""
pass
@contextmanager
def pending_logging() -> Iterator[MemoryHandler]:
"""Context manager to postpone logging all logs temporarily.
For example::
>>> with pending_logging():
>>> logger.warning('Warning message!') # not flushed yet
>>> some_long_process()
>>>
Warning message! # the warning is flushed here
"""
pass
skip_warningiserror = nullcontext
@contextmanager
def prefixed_warnings(prefix: str) -> Iterator[None]:
"""Context manager to prepend prefix to all warning log records temporarily.
For example::
>>> with prefixed_warnings("prefix:"):
>>> logger.warning('Warning message!') # => prefix: Warning message!
.. versionadded:: 2.0
"""
pass
class LogCollector:
def __init__(self) -> None:
self.logs: list[logging.LogRecord] = []
class InfoFilter(logging.Filter):
"""Filter error and warning messages."""
class _RaiseOnWarningFilter(logging.Filter):
"""Raise exception if a warning is emitted."""
def is_suppressed_warning(warning_type: str, sub_type: str, suppress_warnings: Set[str] | Sequence[str]) -> bool:
"""Check whether the warning is suppressed or not."""
pass
class WarningSuppressor(logging.Filter):
"""Filter logs by `suppress_warnings`."""
def __init__(self, app: Sphinx) -> None:
self.app = app
super().__init__()
class MessagePrefixFilter(logging.Filter):
"""Prepend prefix to all log records."""
def __init__(self, prefix: str) -> None:
self.prefix = prefix
super().__init__()
class OnceFilter(logging.Filter):
"""Show the message only once."""
def __init__(self, name: str='') -> None:
super().__init__(name)
self.messages: dict[str, list] = {}
class SphinxLogRecordTranslator(logging.Filter):
"""Converts a log record to one Sphinx expects
* Make a instance of SphinxLogRecord
* docname to path if location given
* append warning type/subtype to message if :confval:`show_warning_types` is ``True``
"""
LogRecordClass: type[logging.LogRecord]
def __init__(self, app: Sphinx) -> None:
self.app = app
super().__init__()
class InfoLogRecordTranslator(SphinxLogRecordTranslator):
"""LogRecordTranslator for INFO level log records."""
LogRecordClass = SphinxInfoLogRecord
class WarningLogRecordTranslator(SphinxLogRecordTranslator):
"""LogRecordTranslator for WARNING level log records."""
LogRecordClass = SphinxWarningLogRecord
class ColorizeFormatter(logging.Formatter):
pass
class SafeEncodingWriter:
"""Stream writer which ignores UnicodeEncodeError silently"""
def __init__(self, stream: IO) -> None:
self.stream = stream
self.encoding = getattr(stream, 'encoding', 'ascii') or 'ascii'
class LastMessagesWriter:
"""Stream writer storing last 10 messages in memory to save trackback"""
def __init__(self, app: Sphinx, stream: IO) -> None:
self.app = app
def setup(app: Sphinx, status: IO, warning: IO) -> None:
"""Setup root logger for Sphinx"""
pass