parse_log_tuples

safir.testing.logging.parse_log_tuples(logger_name, record_tuples, *, ignore_debug=False)

Parse JSON log record tuples into structured data.

This function is intended for use in test suites that want to check the messages logged via structlog in the Safir production profile, meaning that each log message is in JSON. The message bodies are parsed, checked to ensure they’re logged with the provided logger name, stripped of uninteresting data after verification (any timestamp, request_id, or HTTP user agent, all of which vary in uninteresting ways), and then returned as structured records.

Any timestamp in the log message is expected to be within 10 seconds of the current time.

Parameters:
  • logger_name (str) – Only look at messages logged by this logger. Any messages from a different logger will be ignored. The logger is removed from the message before returning it.

  • record_tuples (Iterable[tuple[str, int, str]]) – Tuples of log records consisting of logger name, log level, and message. Normally this argument should be caplog.record_tuples, provided by the pytest caplog fixture.

  • ignore_debug (bool, default: False) – If set to True, filter out all debug messages.

Returns:

List of parsed JSON dictionaries with the common log attributes removed (after validation).

Return type:

list of dict

Raises:

AssertionError – Raised if any of the validation of the log messages fails.