-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split core functionality and support orjson and msgspec (#9)
## Summary of changes ### Refactor common functionality into base module This allows support multiple JSON encoders by having common functionality in `pythonjsonlogger.core` and then specialist formatters for each encoder. This is useful / needed, as not all JSON encoders support the `json.dumps` or `json.JSONEncoder` interfaces exactly. This enables us to support other JSON encoders like orjson and msgspec. In the future we may add support for other encoders. ### Better handling for custom styles Achieved by mimicking `logging.Formatter.__init__` without actually calling it. A code snippet is worth `2**10` words: ```python from pythonjsonlogger.core import BaseJsonLogger class CommaSupport(BaseJsonFormatter): def parse(self) -> list[str]: if isinstance(self._style, str) and self._style == ",": return self._fmt.split(",") return super().parse() f = CommaSupport("message,asctime", style=",", validate=False) ``` ### Rename `jsonlogger` module to `json` module Compatibility is maintained for the moment using `__getattr__` in `__init__`. This is to enable more consistent naming of implementation specific module names. It also stops throwing around the word "logger" when this module only contains formatters. ### Add support for orjson [orjson](https://github.com/ijl/orjson) is a high performance (and more JSON spec correct) encoder. Given how many logging calls may occur - having a performant formatter available is important. This includes ensuring it is covered in tests on appropriate platforms. Note: orjson is not supported on pypy, and currently does not build for py313. ### Add support for msgspec [msgspec](https://jcristharif.com/msgspec/index.html) is another library containing a high performance JSON encoder. Note: msgspec is not supported on pypy, and currently does not build for py313. ### Drops python 3.7 support This is primary due do making use of the [`validate`](https://docs.python.org/3/library/logging.html#formatter-objects) argument. I was also having issues with CI because python 3.7 is not support on most "latest"
- Loading branch information
Showing
15 changed files
with
1,126 additions
and
659 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
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
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,40 @@ | ||
### IMPORTS | ||
### ============================================================================ | ||
## Future | ||
|
||
## Standard Library | ||
import warnings | ||
|
||
## Installed | ||
|
||
## Application | ||
import pythonjsonlogger.json | ||
|
||
### CONSTANTS | ||
### ============================================================================ | ||
try: | ||
import orjson | ||
|
||
ORJSON_AVAILABLE = True | ||
except ImportError: | ||
ORJSON_AVAILABLE = False | ||
|
||
|
||
try: | ||
import msgspec | ||
|
||
MSGSPEC_AVAILABLE = True | ||
except ImportError: | ||
MSGSPEC_AVAILABLE = False | ||
|
||
|
||
### DEPRECATED COMPATIBILITY | ||
### ============================================================================ | ||
def __getattr__(name: str): | ||
if name == "jsonlogger": | ||
warnings.warn( | ||
"pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json", | ||
DeprecationWarning, | ||
) | ||
return pythonjsonlogger.json | ||
raise AttributeError(f"module {__name__} has no attribute {name}") |
Oops, something went wrong.