This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): colorful logs for stdlib logging (#242)
* ✨ feat(log): colorful logs for stdlib logging * ♻️ refactor: remove commented code * 🐛 fix(log): json logging * feat(log): EventFilter processor type. A processor class that allows specific keys to be filtered from a log event. Applies the filter to our default processor chain to remove the "color_message" key that is injected into extras by uvicorn. Co-authored-by: Peter Schutt <[email protected]>
- Loading branch information
1 parent
a287dc8
commit ca2ce6f
Showing
4 changed files
with
101 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Logging utilities. | ||
`msgspec_json_renderer()` | ||
A JSON Renderer for structlog using msgspec. | ||
Msgspec doesn't have an API consistent with the stdlib's `json` module, | ||
which is required for structlog's `JSONRenderer`. | ||
`EventFilter` | ||
A structlog processor that removes keys from the log event if they exist. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import msgspec | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Iterable | ||
|
||
from structlog.typing import EventDict, WrappedLogger | ||
|
||
|
||
_encoder = msgspec.json.Encoder() | ||
|
||
|
||
def msgspec_json_renderer(_: WrappedLogger, __: str, event_dict: EventDict) -> bytes: | ||
"""Structlog processor that uses `msgspec` for JSON encoding. | ||
Args: | ||
_ (): | ||
__ (): | ||
event_dict (): The data to be logged. | ||
Returns: | ||
The log event encoded to JSON by msgspec. | ||
""" | ||
return _encoder.encode(event_dict) | ||
|
||
|
||
class EventFilter: | ||
"""Remove keys from the log event. | ||
Add an instance to the processor chain. | ||
Examples | ||
structlog.configure( | ||
..., | ||
processors=[ | ||
..., | ||
EventFilter(["color_message"]), | ||
..., | ||
] | ||
) | ||
""" | ||
|
||
def __init__(self, filter_keys: Iterable[str]) -> None: | ||
""" | ||
Args: | ||
filter_keys: Iterable of string keys to be excluded from the log event. | ||
""" | ||
self.filter_keys = filter_keys | ||
|
||
def __call__(self, _: WrappedLogger, __: str, event_dict: EventDict) -> EventDict: | ||
"""Receive the log event, and filter keys. | ||
Args: | ||
_ (): | ||
__ (): | ||
event_dict (): The data to be logged. | ||
Returns: | ||
The log event with any key in `self.filter_keys` removed. | ||
""" | ||
for key in self.filter_keys: | ||
event_dict.pop(key, None) | ||
return event_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters