Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split core functionality and support orjson and msgspec #9

Merged
merged 27 commits into from
May 5, 2024
Merged

Conversation

nhairs
Copy link
Owner

@nhairs nhairs commented Apr 27, 2024

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:

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 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 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 argument. I was also having issues with CI because python 3.7 is not available on macos-latest as of /recently/.

Thinking more I could use **kwargs to capture the newer arguments, but also effort? Python 3.7 is soon going to be 2 years out of support (python 3.8 is schedule to be EOL this year).

It's probably not a bad time to drop support since new major version. I'll still probably support bug fixes / security fixes for a while longer though (note to self - I'll need to make a 3.x branch...).

edit: 🤦 - I've already said python 3.7 is security fixes only so fine to drop support.

@nhairs nhairs changed the title 4.x Split core functionality and support orjson Apr 29, 2024
@nhairs nhairs changed the title Split core functionality and support orjson Split core functionality and support orjson and msgspec May 3, 2024
pyproject.toml Outdated Show resolved Hide resolved
# dependencies = []
requires-python = ">=3.8"
dependencies = [
"typing_extensions",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to specify when to install this?

@nhairs nhairs merged commit 49f69fb into main May 5, 2024
28 checks passed
@nhairs nhairs deleted the dev branch May 5, 2024 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant