|
5 | 5 | import logging
|
6 | 6 | import re
|
7 | 7 |
|
8 |
| -log = logging.getLogger(__name__) |
9 |
| - |
10 | 8 |
|
11 | 9 | class Formatter(logging.Formatter):
|
12 | 10 | """
|
@@ -89,25 +87,37 @@ def __init__(self, regex: str, msg: str):
|
89 | 87 | self.msg = msg
|
90 | 88 | self._count = 0
|
91 | 89 |
|
92 |
| - def inspect(self, record: logging.LogRecord): |
| 90 | + def inspect(self, record: logging.LogRecord) -> bool: |
93 | 91 | """
|
94 | 92 | Inspect a LogRecord to determine if it matches this MetaWarning.
|
95 | 93 |
|
96 | 94 | Parameters
|
97 | 95 | ----------
|
98 | 96 | record: logging.LogRecord
|
99 | 97 | The LogRecord to inspect.
|
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + bool |
| 102 | + True if `record` matches this MetaWarning and False otherwise. |
100 | 103 | """
|
101 | 104 | if self.regex.search(record.msg):
|
102 | 105 | self._count += 1
|
| 106 | + return True |
| 107 | + return False |
103 | 108 |
|
104 |
| - def warn(self): |
| 109 | + def warn(self, logger: logging.Logger): |
105 | 110 | """
|
106 | 111 | Produce the warning associated with this MetaWarning.
|
| 112 | +
|
| 113 | + Parameters |
| 114 | + ---------- |
| 115 | + log: logging.Logger |
| 116 | + The Logger that should be used to generate the MetaWarning. |
107 | 117 | """
|
108 | 118 | if self._count == 0:
|
109 | 119 | return
|
110 |
| - log.warning(self.msg.format(self._count)) |
| 120 | + logger.warning(self.msg.format(self._count)) |
111 | 121 |
|
112 | 122 |
|
113 | 123 | class WarningAggregator(logging.Filter):
|
@@ -161,10 +171,15 @@ def filter(self, record: logging.LogRecord) -> bool:
|
161 | 171 | meta_warning.inspect(record)
|
162 | 172 | return True
|
163 | 173 |
|
164 |
| - def warn(self): |
| 174 | + def warn(self, logger: logging.Logger): |
165 | 175 | """
|
166 | 176 | Produce the warning associated with any MetaWarning(s) that were
|
167 | 177 | matched by this WarningAggregator.
|
| 178 | +
|
| 179 | + Parameters |
| 180 | + ---------- |
| 181 | + logger: logging.Logger |
| 182 | + The Logger that should be used to generate the MetaWarning(s). |
168 | 183 | """
|
169 | 184 | for meta_warning in self.meta_warnings:
|
170 |
| - meta_warning.warn() |
| 185 | + meta_warning.warn(logger) |
0 commit comments