-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds `StructlogCapturer` and related docs. Follows `loguru` extension structure, borrowing inspiration from the structlog testing tools. I've matched the suggested version range in hynek/structlog#596. Closes #108
- Loading branch information
1 parent
4f2e4f0
commit e7349c6
Showing
15 changed files
with
304 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
:mod:`logot.structlog` | ||
======================= | ||
|
||
.. automodule:: logot.structlog | ||
|
||
|
||
API reference | ||
------------- | ||
|
||
.. autoclass:: StructlogCapturer |
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 |
---|---|---|
|
@@ -47,6 +47,7 @@ Supported frameworks: | |
:maxdepth: 1 | ||
|
||
loguru | ||
structlog | ||
|
||
.. seealso:: | ||
|
||
|
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,95 @@ | ||
Using with :mod:`structlog` | ||
============================ | ||
|
||
.. currentmodule:: logot | ||
|
||
:mod:`logot` makes it easy to capture logs from :mod:`structlog`: | ||
|
||
.. code:: python | ||
from logot.structlog import StructlogCapturer | ||
with Logot(capturer=StructlogCapturer).capturing() as logot: | ||
do_something() | ||
logot.assert_logged(logged.info("App started")) | ||
:mod:`logot` preserves the preconfigured :mod:`structlog` processor chain. Events are captured at the end of the chain, | ||
but before the final processor, as it is responsible for emitting the log event to the underlying logging system. For | ||
more information, see the | ||
`structlog documentation <https://www.structlog.org/en/stable/processors.html#adapting-and-rendering>`_. | ||
|
||
|
||
Installing | ||
---------- | ||
|
||
Ensure :mod:`logot` is installed alongside a compatible :mod:`structlog` version by adding the ``structlog`` extra: | ||
|
||
.. code:: bash | ||
pip install 'logot[structlog]' | ||
.. seealso:: | ||
|
||
See :ref:`installing-extras` usage guide. | ||
|
||
|
||
Enabling for :mod:`pytest` | ||
-------------------------- | ||
|
||
Enable :mod:`structlog` support in your :external+pytest:doc:`pytest configuration <reference/customize>`: | ||
|
||
.. code:: ini | ||
# pytest.ini or .pytest.ini | ||
[pytest] | ||
logot_capturer = logot.structlog.StructlogCapturer | ||
.. code:: toml | ||
# pyproject.toml | ||
[tool.pytest.ini_options] | ||
logot_capturer = "logot.structlog.StructlogCapturer" | ||
.. seealso:: | ||
|
||
See :doc:`/using-pytest` usage guide. | ||
|
||
|
||
Enabling for :mod:`unittest` | ||
---------------------------- | ||
|
||
Enable :mod:`structlog` support in your :class:`logot.unittest.LogotTestCase`: | ||
|
||
.. code:: python | ||
from logot.structlog import StructlogCapturer | ||
class MyAppTest(LogotTestCase): | ||
logot_capturer = StructlogCapturer | ||
.. seealso:: | ||
|
||
See :doc:`/using-unittest` usage guide. | ||
|
||
|
||
Enabling manually | ||
----------------- | ||
|
||
Enable :mod:`structlog` support for your :class:`Logot` instance: | ||
|
||
.. code:: python | ||
from logot.structlog import StructlogCapturer | ||
logot = Logot(capturer=StructlogCapturer) | ||
Enable :mod:`structlog` support for a single :meth:`Logot.capturing` call: | ||
|
||
.. code:: python | ||
with Logot().capturing(capturer=StructlogCapturer) as logot: | ||
do_something() | ||
.. seealso:: | ||
|
||
See :class:`Logot` and :meth:`Logot.capturing` API reference. |
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,53 @@ | ||
from __future__ import annotations | ||
|
||
from functools import partial | ||
|
||
import structlog | ||
from structlog.processors import NAME_TO_LEVEL | ||
from structlog.types import EventDict, WrappedLogger | ||
|
||
from logot._capture import Captured | ||
from logot._logot import Capturer, Logot | ||
from logot._typing import Level, Name | ||
|
||
|
||
class StructlogCapturer(Capturer): | ||
""" | ||
A :class:`logot.Capturer` implementation for :mod:`structlog`. | ||
""" | ||
|
||
__slots__ = ("_old_processors",) | ||
|
||
def start_capturing(self, logot: Logot, /, *, level: Level, name: Name) -> None: | ||
config = structlog.get_config() | ||
processors = config["processors"] | ||
self._old_processors = processors | ||
|
||
if isinstance(level, str): | ||
levelno = NAME_TO_LEVEL[level.lower()] | ||
else: | ||
levelno = level | ||
|
||
# We need to insert our processor before the last processor, as this is the processor that transforms the | ||
# `event_dict` into the final log message. As this depends on the wrapped logger's formatting requirements, | ||
# it can interfere with our capturing. | ||
# See https://www.structlog.org/en/stable/processors.html#adapting-and-rendering | ||
structlog.configure( | ||
processors=[*processors[:-1], partial(_processor, logot=logot, name=name, levelno=levelno), processors[-1]] | ||
) | ||
|
||
def stop_capturing(self) -> None: | ||
structlog.configure(processors=self._old_processors) | ||
|
||
|
||
def _processor( | ||
logger: WrappedLogger, method_name: str, event_dict: EventDict, *, logot: Logot, name: Name, levelno: int | ||
) -> EventDict: | ||
msg = event_dict["event"] | ||
level = method_name.upper() | ||
event_levelno = NAME_TO_LEVEL[method_name] | ||
|
||
if getattr(logger, "name", None) == name and event_levelno >= levelno: | ||
logot.capture(Captured(level, msg, levelno=event_levelno)) | ||
|
||
return event_dict |
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,10 @@ | ||
""" | ||
Integration API for :mod:`structlog`. | ||
.. seealso:: | ||
See :doc:`/integrations/structlog` usage guide. | ||
""" | ||
from __future__ import annotations | ||
|
||
from logot._structlog import StructlogCapturer as StructlogCapturer |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.