Log capturing#

logot makes it easy to capture logs from the stdlib logging module:

with Logot().capturing() as logot:
   app.start()
   logot.wait_for(logged.info("App started"))

Note

If using pytest, you can probably just use the pre-configured logot fixture included in the bundled pytest plugin and skip manually configuring log capture. 💪

Capturing logging logs#

The Logot.capturing() method defaults to capturing all records from the root logger. Customize this with the level and logger arguments to Logot.capturing():

with Logot().capturing(level=logging.WARNING, logger="app") as logot:
   app.start()
   logot.wait_for(logged.info("App started"))

For advanced use-cases, multiple Logot.capturing() calls on the same Logot instance are supported. Be careful to avoid capturing duplicate logs with overlapping calls to Logot.capturing()!

See also

See Logot and Logot.capturing() API reference.

Capturing 3rd-party logs#

Any 3rd-party logging library can be integrated with logot by sending Captured logs to Logot.capture():

def on_foo_log(logot: Logot, record: FooRecord) -> None:
   logot.capture(Captured(record.levelname, record.msg, levelno=record.levelno))

foo_logger.add_handler(on_foo_log)

Note

Using a context manager to set up and tear down log capture for every test run is highly recommended!

See also

See Captured and Logot.capture() API reference.