|
23 | 23 |
|
24 | 24 | from makefun import wraps |
25 | 25 |
|
| 26 | +from annotated_logger.filter import AnnotatedFilter |
26 | 27 | from annotated_logger.plugins import BasePlugin |
27 | 28 |
|
28 | 29 | if TYPE_CHECKING: # pragma: no cover |
@@ -98,16 +99,12 @@ class AnnotatedClass(Protocol[C_co]): |
98 | 99 | "handlers": { |
99 | 100 | "annotated_handler": { |
100 | 101 | "class": "logging.StreamHandler", |
101 | | - "filters": ["annotated_filter"], |
102 | 102 | "formatter": "annotated_formatter", |
103 | 103 | }, |
104 | 104 | }, |
105 | 105 | "formatters": { |
106 | 106 | "annotated_formatter": { |
107 | 107 | "class": "pythonjsonlogger.jsonlogger.JsonFormatter", # pragma: no mutate |
108 | | - # Note that this format string uses `time` and `level` which are |
109 | | - # set by the renamer plugin. Because the handler is using the |
110 | | - # annotated_filter the plugings will be run and the fields will be renamed |
111 | 108 | "format": "{created} {levelname} {name} {message}", # pragma: no mutate |
112 | 109 | "style": "{", |
113 | 110 | }, |
@@ -167,61 +164,6 @@ def __next__(self) -> T: |
167 | 164 | return value |
168 | 165 |
|
169 | 166 |
|
170 | | -class AnnotatedFilter(logging.Filter): |
171 | | - """Filter class that stores the annotations and plugins.""" |
172 | | - |
173 | | - def __init__( |
174 | | - self, |
175 | | - annotations: Annotations, |
176 | | - runtime_annotations: Annotations, |
177 | | - class_annotations: Annotations, |
178 | | - plugins: list[BasePlugin], |
179 | | - ) -> None: |
180 | | - """Store the annotations, attributes and plugins.""" |
181 | | - self.annotations = annotations |
182 | | - self.class_annotations = class_annotations |
183 | | - self.runtime_annotations = runtime_annotations or {} |
184 | | - self.plugins = plugins |
185 | | - |
186 | | - # This allows plugins to determine what fields were added by the user |
187 | | - # vs the ones native to the log record |
188 | | - # TODO(crimsonknave): Make a test for this # noqa: TD003, FIX002 |
189 | | - self.base_attributes = logging.makeLogRecord({}).__dict__ # pragma: no mutate |
190 | | - |
191 | | - def _all_annotations(self, record: logging.LogRecord) -> Annotations: |
192 | | - annotations = {} |
193 | | - # Using copy might be better, but, we don't want to add |
194 | | - # the runtime annotations to the stored annotations |
195 | | - annotations.update(self.class_annotations) |
196 | | - annotations.update(self.annotations) |
197 | | - for key, function in self.runtime_annotations.items(): |
198 | | - annotations[key] = function(record) |
199 | | - annotations["annotated"] = True |
200 | | - return annotations |
201 | | - |
202 | | - def filter(self, record: logging.LogRecord) -> bool: |
203 | | - """Add the annotations to the record and allow plugins to filter the record. |
204 | | -
|
205 | | - The `filter` method is called on each plugin in the order they are listed. |
206 | | - The plugin is then able to maniuplate the record object before the next plugin |
207 | | - sees it. Returning False from the filter method will stop the evaluation and |
208 | | - the log record won't be emitted. |
209 | | - """ |
210 | | - record.__dict__.update(self._all_annotations(record)) |
211 | | - for plugin in self.plugins: |
212 | | - try: |
213 | | - result = plugin.filter(record) |
214 | | - except Exception: # noqa: BLE001 |
215 | | - failed_plugins = record.__dict__.get("failed_plugins", []) |
216 | | - failed_plugins.append(str(plugin.__class__)) |
217 | | - record.__dict__["failed_plugins"] = failed_plugins |
218 | | - result = True |
219 | | - |
220 | | - if not result: |
221 | | - return False |
222 | | - return True |
223 | | - |
224 | | - |
225 | 167 | class AnnotatedAdapter(logging.LoggerAdapter): # pyright: ignore[reportMissingTypeArgument] |
226 | 168 | """Adapter that provides extra methods.""" |
227 | 169 |
|
|
0 commit comments