Using with structlog¶

logot makes it easy to capture logs from structlog:

from logot.structlog import StructlogCapturer

with Logot(capturer=StructlogCapturer).capturing() as logot:
   do_something()
   logot.assert_logged(logged.info("Something was done"))

logot will capture logs before any processors are invoked. Any filtering, formatting, or other processing from the processor chain will not be applied to the captured logs.

Note

logot.structlog.StructlogCapturer works by changing the structlog configuration. If you have cache_logger_on_first_use enabled in your structlog.configure() or structlog.wrap_logger() call for performance reasons, you will need to disable it during tests to enable log capturing.

Installing¶

Ensure logot is installed alongside a compatible structlog version by adding the structlog extra:

pip install 'logot[structlog]'

See also

See Installing package extras usage guide.

Enabling for pytest¶

Enable structlog support in your pytest configuration:

# pytest.ini or .pytest.ini
[pytest]
logot_capturer = logot.structlog.StructlogCapturer
# pyproject.toml
[tool.pytest.ini_options]
logot_capturer = "logot.structlog.StructlogCapturer"

See also

See Using with pytest usage guide.

Enabling for unittest¶

Enable structlog support in your logot.unittest.LogotTestCase:

from logot.structlog import StructlogCapturer

class MyAppTest(LogotTestCase):
   logot_capturer = StructlogCapturer

See also

See Using with unittest usage guide.

Enabling manually¶

Enable structlog support for your Logot instance:

from logot.structlog import StructlogCapturer

logot = Logot(capturer=StructlogCapturer)

Enable structlog support for a single Logot.capturing() call:

with Logot().capturing(capturer=StructlogCapturer) as logot:
   do_something()

See also

See Logot and Logot.capturing() API reference.