litestar-org/litestar

Enhancement: add `default_json_serializer` to `__all__` in `litestar.logging.config`

keongalvin opened this issue · 0 comments

Summary

When customizing StructLoggingConfig with structlog.processors.JSONRenderer processor, it is convenient to use default_json_serializer.

However the function is currently not in __all__, causing linting errors.

A quick solution would be to add it to module's __all__, but maybe we can re-use encode_json?

Basic Example

Here's an example of using structlog.processors.JSONRenderer.

import structlog
from litestar import Litestar
from litestar import get
from litestar.logging import StructLoggingConfig
from litestar.logging.config import default_json_serializer
from litestar.plugins.structlog import StructlogConfig
from litestar.plugins.structlog import StructlogPlugin


logger = structlog.get_logger(__name__)


@get(path="/health", sync_to_thread=False)
def hello() -> dict:
    logger.info("Hello World!")
    return {"hello": "world"}


app = Litestar(
    route_handlers=[hello],
    plugins=[
        StructlogPlugin(
            config=StructlogConfig(
                structlog_logging_config=StructLoggingConfig(
                    processors=[
                        structlog.processors.TimeStamper(fmt="iso"),
                        structlog.processors.JSONRenderer(default_json_serializer),
                    ],
                )
            )
        )
    ],
)

Drawbacks and Impact

No response

Unresolved questions

No response


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar