Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
chore(deps): depend on released version of structlog.
Browse files Browse the repository at this point in the history
This commit should be reverted once structlog makes next release.
  • Loading branch information
peterschutt committed Nov 7, 2022
1 parent 9beca43 commit 8268484
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ saq = "*"
sentry-sdk = "*"
sqlalchemy = "==2.0.0b3"
starlite = "~=1.32"
# waiting on release to include https://github.com/hynek/structlog/pull/457
structlog = { git = "https://github.com/hynek/structlog.git", rev = "6b06c062dbcd166bfba0da407e9af63432bedea2" }
structlog = "*"

[tool.poetry.urls]
GitHub = "https://github.com/topsport-com-au"
Expand Down
42 changes: 40 additions & 2 deletions src/starlite_saqlalchemy/log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import asyncio
import contextvars
import logging
import sys
from typing import TYPE_CHECKING
Expand All @@ -16,8 +18,9 @@

if TYPE_CHECKING:
from collections.abc import Sequence
from typing import Any

from structlog.typing import Processor
from structlog.types import Processor

__all__ = (
"default_processors",
Expand Down Expand Up @@ -46,6 +49,41 @@
)


def _make_filtering_bound_logger(min_level: int) -> type[structlog.types.FilteringBoundLogger]:
"""Wraps structlog's `FilteringBoundLogger` to add the `alog()` method,
which is in structlog's main branch, but not yet released.
Args:
min_level: Log level as an integer.
Returns:
Structlog's `FilteringBoundLogger` with an `alog()` method that does its work off the event
loop.
"""
filtering_bound_logger = structlog.make_filtering_bound_logger(min_level=min_level)

# pylint: disable=too-few-public-methods
class _WrappedFilteringBoundLogger(filtering_bound_logger): # type:ignore[misc,valid-type]
async def alog(self: Any, level: int, event: str, *args: Any, **kw: Any) -> Any:
"""This method will exist in the next release of structlog."""
if level < min_level:
return None
# pylint: disable=protected-access
name = structlog._log_levels._LEVEL_TO_NAME[level] # pyright: ignore

ctx = contextvars.copy_context()
return await asyncio.get_running_loop().run_in_executor(
None,
lambda: ctx.run(
lambda: self._proxy_to_logger( # type:ignore[no-any-return]
name, event % args, **kw
)
),
)

return _WrappedFilteringBoundLogger


def configure(processors: Sequence[Processor]) -> None:
"""Call to configure `structlog` on app startup.
Expand All @@ -58,7 +96,7 @@ def configure(processors: Sequence[Processor]) -> None:
cache_logger_on_first_use=True,
logger_factory=structlog.BytesLoggerFactory(),
processors=processors,
wrapper_class=structlog.make_filtering_bound_logger(settings.log.LEVEL),
wrapper_class=_make_filtering_bound_logger(settings.log.LEVEL),
)


Expand Down
2 changes: 1 addition & 1 deletion src/starlite_saqlalchemy/log/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from starlite.connection import Request
from starlite.datastructures import State
from starlite.types.asgi_types import ASGIApp, Message, Receive, Scope, Send
from structlog.typing import EventDict, WrappedLogger
from structlog.types import EventDict, WrappedLogger

LOGGER = structlog.get_logger()

Expand Down

0 comments on commit 8268484

Please sign in to comment.