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.
Adds a custom rendering processor to the processor chain that uses msgspec encoder. Closes #166
- Loading branch information
1 parent
2a439e7
commit 818539d
Showing
2 changed files
with
36 additions
and
9 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 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,31 @@ | ||
"""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`. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import msgspec | ||
|
||
if TYPE_CHECKING: | ||
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) |