supercontest.util.logging

Utilities to assist with logging for the entire application.

Module Attributes

HANDLER_NAME

The name of the one handler supercontest.configure_logging() installs, so a second call can find and replace it.

request_id

The id of the request being served, or None outside one.

Functions

create_handler

Creates the standard streamhandler, with supercontest-specific format.

print_data

Takes a list of data and prints the desired keys in columns as defined by the input widths.

show_loggers

Helpful printer to show the tree of loggers in the current app.

Classes

JsonFormatter

One JSON object per line: what CloudWatch Logs Insights reads field by field.

RequestIdFilter

Stamps each record with the id of the request it was written in.

supercontest.util.logging.HANDLER_NAME = 'supercontest'

The name of the one handler supercontest.configure_logging() installs, so a second call can find and replace it.

class supercontest.util.logging.JsonFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)

Bases: Formatter

One JSON object per line: what CloudWatch Logs Insights reads field by field.

format(record: LogRecord) → str

The record as JSON, its extra= fields included.

Parameters:

record – The record.

Returns:

The line.

class supercontest.util.logging.RequestIdFilter(name='')

Bases: Filter

Stamps each record with the id of the request it was written in.

filter(record: LogRecord) → bool

Adds request_id; never drops a record.

Parameters:

record – The record.

Returns:

True.

supercontest.util.logging.create_handler(json_lines: bool = False) → Handler

Creates the standard streamhandler, with supercontest-specific format.

Then attach it to any logger. Example: logging.getLogger("mylogger").addHandler(create_handler())

Parameters:

json_lines – JSON, for deployed logs a query reads; else text, for a terminal.

Returns:

A formatted stream handler.

supercontest.util.logging.print_data(data: list[dict[str, Any]], keys: list[str], widths: list[int]) → None

Takes a list of data and prints the desired keys in columns as defined by the input widths. Separates each column by a space, and surrounds each column with brackets. This intentionally prints to stdout, rather than a custom logger.

Parameters:
  • data – The data to print, each item a row, in order.

  • keys – The keys to print as cols, in order.

  • widths – The width of the cols to print, in order.

supercontest.util.logging.request_id: ContextVar = <ContextVar name='sbsc_request_id' default=None>

The id of the request being served, or None outside one. Set by the API’s request middleware (supercontest.api.observability); here so that the formatters, which every entry point shares, need not import the API.

supercontest.util.logging.show_loggers(attrs: list[str] | None = None) → None

Helpful printer to show the tree of loggers in the current app.

Will pretty-print them to stdout alphabetically, with the desired attributes. Some rules:

  • If the attr doesn’t exist for a logger, it will print None.

  • If the attr is a list, it will print a joined string of the items.

  • If the attr is “level”, it will cast to the logger’s name (not number, the default).

  • If the attr is anything else, it will just str() the attr value.

Parameters:

attrs – The columns to print for each logger. Defaults to name, level, parent, handlers, propagate.