Skip to content

Commit

Permalink
fix: additional structlog fixes (#2985)
Browse files Browse the repository at this point in the history
* Bump version to 2.6.0

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat: `structlog` plugin & bug fixes (#2943)

* feat(channels): Postgres backends (#2803)

* wip

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* some debugging

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* formatting

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* use a separate connection to publish/listen

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* reintroduce flaky

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix typing

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add psycopg backend

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix backend issues

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Undo test debugging changes

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* mark groups

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Ensure channel names ar quoted

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* sleep debugging

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* update docs

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add missing test

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix docs link

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add missing listener test

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Formatting

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix test typing

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix some coverage issue

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* test skip sourcery

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* test(channels): Improve channels testing (#2838)

* Improve channels testing

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* chore(typing): various pyright issues (#2897)

Fix various pyright issues

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* fix(channels): Trailing messages after unsubscribes (#2894)

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(cli): Add ``--schema`` and ``--exclude`` option to route CLI. (#2886)

* add exclude and schema cli options to route.

* updates per linting, mypy, and etc.

* fix some more mypy stuff.

* fix issue with linting.

* add doc for route cli options.

* fix issue with python3.8 not liking dict type.

* add exclude and schema cli options to route.

* updates per linting, mypy, and etc.

* fix some more mypy stuff.

* fix issue with linting.

* add doc for route cli options.

* fix issue with python3.8 not liking dict type.

* Update docs/usage/cli.rst

* Update litestar/cli/_utils.py

* fix malformed docs table.

---------

Co-authored-by: Jacob Coffee <jacob@z7x.org>

* test(CLI): Fix xdist issue (#2931)

Fix test for xdist

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(core): Replace `anyio.to_thread.run_sync` with native versions (#2937)

Replace anyio.to_thread.run_sync with native versions

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat: example app using structlog

* fix: updated structlog with request logging

* fix: lazy initialized structlog fix

* feat: add structlog plugin

* fix: adds `set_level` to all Logging configurations

* fix: check that the object has the `setLevel` method before calling

* feat: adds test for plugin

* fix: parameter naming for `set_level` abstract method

* feat(channels): Postgres backends (#2803)

* wip

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* some debugging

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* formatting

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* use a separate connection to publish/listen

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* reintroduce flaky

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix typing

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add psycopg backend

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix backend issues

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Undo test debugging changes

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* mark groups

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Ensure channel names ar quoted

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* sleep debugging

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* update docs

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add missing test

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix docs link

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add missing listener test

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Formatting

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix test typing

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix some coverage issue

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* test skip sourcery

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* test(channels): Improve channels testing (#2838)

* Improve channels testing

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* chore(typing): various pyright issues (#2897)

Fix various pyright issues

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* fix(channels): Trailing messages after unsubscribes (#2894)

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(cli): Add ``--schema`` and ``--exclude`` option to route CLI. (#2886)

* add exclude and schema cli options to route.

* updates per linting, mypy, and etc.

* fix some more mypy stuff.

* fix issue with linting.

* add doc for route cli options.

* fix issue with python3.8 not liking dict type.

* add exclude and schema cli options to route.

* updates per linting, mypy, and etc.

* fix some more mypy stuff.

* fix issue with linting.

* add doc for route cli options.

* fix issue with python3.8 not liking dict type.

* Update docs/usage/cli.rst

* Update litestar/cli/_utils.py

* fix malformed docs table.

---------

Co-authored-by: Jacob Coffee <jacob@z7x.org>

* test(CLI): Fix xdist issue (#2931)

Fix test for xdist

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(core): Replace `anyio.to_thread.run_sync` with native versions (#2937)

Replace anyio.to_thread.run_sync with native versions

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(channels): Postgres backends (#2803)

* wip

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* some debugging

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* formatting

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* use a separate connection to publish/listen

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* reintroduce flaky

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix typing

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add psycopg backend

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix backend issues

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Undo test debugging changes

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* mark groups

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Ensure channel names ar quoted

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* sleep debugging

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* update docs

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add missing test

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix docs link

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add missing listener test

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Formatting

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix test typing

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Fix some coverage issue

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* test skip sourcery

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* test(channels): Improve channels testing (#2838)

* Improve channels testing

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* chore(typing): various pyright issues (#2897)

Fix various pyright issues

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* fix(channels): Trailing messages after unsubscribes (#2894)

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(cli): Add ``--schema`` and ``--exclude`` option to route CLI. (#2886)

* add exclude and schema cli options to route.

* updates per linting, mypy, and etc.

* fix some more mypy stuff.

* fix issue with linting.

* add doc for route cli options.

* fix issue with python3.8 not liking dict type.

* add exclude and schema cli options to route.

* updates per linting, mypy, and etc.

* fix some more mypy stuff.

* fix issue with linting.

* add doc for route cli options.

* fix issue with python3.8 not liking dict type.

* Update docs/usage/cli.rst

* Update litestar/cli/_utils.py

* fix malformed docs table.

---------

Co-authored-by: Jacob Coffee <jacob@z7x.org>

* test(CLI): Fix xdist issue (#2931)

Fix test for xdist

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat(core): Replace `anyio.to_thread.run_sync` with native versions (#2937)

Replace anyio.to_thread.run_sync with native versions

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat: structlog detects tty by default

* chore: linting fixes

* fix: color code correction

* fix: adjusted color code to be more visible

* fix: additional config settings

* feat: enable pretty-print in TTY

* fix: apply rich configuration

* fix: updated formatting to align with other messages

* chore: trim whitespace

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <provinzkraut@posteo.de>
Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: andrew do <andrewddo@gmail.com>
Co-authored-by: Jacob Coffee <jacob@z7x.org>

* feat: allow using custom `CompressionFacade` implementations (#2952)

* refactor: move the different compressions into their own implementations

* feat: check if encoding given by the facade is accepted

* Bump version to 2.6.0

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* feat: allow use of custom CompressionFacade implementations

* feat: allow gzip to be used as fallback for any backend

* docs: clarify 'backend'

* test: rename test to a clearer name

* docs: add docstring for 'CompressionFacade.encoding'

* fix: explicitly specify facade type

* fix: only import BrotliCompression if backend is brotli

If the backend is not brotli, then the user may not have installed brotli which would result in an incorrect
MissingDependency exception.

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <provinzkraut@posteo.de>
Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com>

* feat: Add `reload-include` and `reload-exclude` from uvicorn to CLI (#2973)

* Add reload-include and reload-exclude feature

* Update documentation

* Fix tests, update documentation

* feat: allow `root` logger configuration to be disabled (#2969)

* docs: Fix reference error (#2983)

Fix doc reference error

* fix: correctly render stdlib logs as string instead of bytes

* feat: add missing timestamper to standard logging for structlog

* feat: filter out `color_message` by default

* feat: add nocover for dev logger

* feat: adds test for `TTY` config of structlog

* feat: increased coverage

* fix: remove incorrect call to `get` a plugin

* feat: additional coverage

* fix: remove unnecessary mixin

* feat: additional coverage

* fix: add ignore on lines that are actually covered

* feat: add deprecated function

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <provinzkraut@posteo.de>
Co-authored-by: andrew do <andrewddo@gmail.com>
Co-authored-by: Jacob Coffee <jacob@z7x.org>
Co-authored-by: guacs <126393040+guacs@users.noreply.github.com>
Co-authored-by: FergusMok <FergusMok1@gmail.com>
  • Loading branch information
7 people committed Jan 23, 2024
1 parent 0ea4a3e commit 585d441
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 83 deletions.
89 changes: 71 additions & 18 deletions litestar/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@

from litestar.exceptions import ImproperlyConfiguredException, MissingDependencyException
from litestar.serialization import encode_json
from litestar.serialization.msgspec_hooks import _msgspec_json_encoder
from litestar.utils.deprecation import deprecated

__all__ = ("BaseLoggingConfig", "LoggingConfig", "StructLoggingConfig")


if TYPE_CHECKING:
from collections.abc import Iterable
from typing import NoReturn

# these imports are duplicated on purpose so sphinx autodoc can find and link them
from structlog.types import BindableLogger, Processor, WrappedLogger
from structlog.typing import EventDict

from litestar.types import Logger, Scope
from litestar.types.callable_types import ExceptionLoggingHandler, GetLogger
Expand Down Expand Up @@ -250,8 +254,55 @@ def set_level(logger: Logger, level: int) -> None:
logger.setLevel(level)


def default_json_serializer(value: Any, default: Callable[[Any], Any] | None = None) -> bytes:
return encode_json(value=value, serializer=default)
class StructlogEventFilter:
"""Remove keys from the log event.
Add an instance to the processor chain.
.. code-block:: python
:caption: Examples
structlog.configure(
...,
processors=[
...,
EventFilter(["color_message"]),
...,
],
)
"""

def __init__(self, filter_keys: Iterable[str]) -> None:
"""Initialize the EventFilter.
Args:
filter_keys: Iterable of string keys to be excluded from the log event.
"""
self.filter_keys = filter_keys

def __call__(self, _: WrappedLogger, __: str, event_dict: EventDict) -> EventDict:
"""Receive the log event, and filter keys.
Args:
_ ():
__ ():
event_dict (): The data to be logged.
Returns:
The log event with any key in `self.filter_keys` removed.
"""
for key in self.filter_keys:
event_dict.pop(key, None)
return event_dict


def default_json_serializer(value: EventDict, **_: Any) -> bytes:
return _msgspec_json_encoder.encode(value)


def stdlib_json_serializer(value: EventDict, **_: Any) -> str: # pragma: no cover
return _msgspec_json_encoder.encode(value).decode("utf-8")


def default_structlog_processors(as_json: bool = True) -> list[Processor]: # pyright: ignore
Expand Down Expand Up @@ -297,13 +348,19 @@ def default_structlog_standard_lib_processors(as_json: bool = True) -> list[Proc

if as_json:
return [
structlog.processors.TimeStamper(fmt="iso"),
structlog.stdlib.add_log_level,
structlog.stdlib.ExtraAdder(),
structlog.processors.JSONRenderer(serializer=default_json_serializer),
StructlogEventFilter(["color_message"]),
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
structlog.processors.JSONRenderer(serializer=stdlib_json_serializer),
]
return [
structlog.processors.TimeStamper(fmt="iso"),
structlog.stdlib.add_log_level,
structlog.stdlib.ExtraAdder(),
StructlogEventFilter(["color_message"]),
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
structlog.dev.ConsoleRenderer(
colors=True, exception_formatter=RichTracebackFormatter(max_frames=1, show_locals=False, width=80)
),
Expand All @@ -312,21 +369,6 @@ def default_structlog_standard_lib_processors(as_json: bool = True) -> list[Proc
return []


def default_wrapper_class(log_level: int = INFO) -> type[BindableLogger] | None: # pyright: ignore
"""Set the default wrapper class for structlog.
Returns:
An optional wrapper class.
"""

try:
import structlog

return structlog.make_filtering_bound_logger(log_level)
except ImportError:
return None


def default_logger_factory(as_json: bool = True) -> Callable[..., WrappedLogger] | None:
"""Set the default logger factory for structlog.
Expand Down Expand Up @@ -438,3 +480,14 @@ def set_level(logger: Logger, level: int) -> None:
structlog.configure(wrapper_class=structlog.make_filtering_bound_logger(level))
except ImportError:
""""""
return


@deprecated(version="2.6.0", removal_in="3.0.0", alternative="`StructLoggingConfig.set_level`")
def default_wrapper_class(log_level: int = INFO) -> type[BindableLogger] | None: # pragma: no cover # pyright: ignore
try: # pragma: no cover
import structlog

return structlog.make_filtering_bound_logger(log_level)
except ImportError:
return None
13 changes: 4 additions & 9 deletions litestar/plugins/structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
from litestar.cli._utils import console
from litestar.logging.config import StructLoggingConfig
from litestar.middleware.logging import LoggingMiddlewareConfig
from litestar.plugins import CLIPluginProtocol, InitPluginProtocol
from litestar.plugins import InitPluginProtocol

if TYPE_CHECKING:
from click import Group

from litestar.config.app import AppConfig


Expand All @@ -24,7 +22,7 @@ class StructlogConfig:
"""Enable request logging."""


class StructlogPlugin(InitPluginProtocol, CLIPluginProtocol):
class StructlogPlugin(InitPluginProtocol):
"""Structlog Plugin."""

__slots__ = ("_config",)
Expand All @@ -35,9 +33,6 @@ def __init__(self, config: StructlogConfig | None = None) -> None:
self._config = config
super().__init__()

def on_cli_init(self, cli: Group) -> None:
return super().on_cli_init(cli)

def on_app_init(self, app_config: AppConfig) -> AppConfig:
"""Structlog Plugin
Expand All @@ -54,8 +49,8 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
else:
app_config.logging_config = self._config.structlog_logging_config
app_config.logging_config.configure()
if self._config.structlog_logging_config.standard_lib_logging_config is not None:
self._config.structlog_logging_config.standard_lib_logging_config.configure()
if self._config.structlog_logging_config.standard_lib_logging_config is not None: # pragma: no cover
self._config.structlog_logging_config.standard_lib_logging_config.configure() # pragma: no cover
if self._config.enable_middleware_logging:
app_config.middleware.append(self._config.middleware_logging_config.middleware)
return app_config # pragma: no cover
Loading

0 comments on commit 585d441

Please sign in to comment.