Skip to content

OSSS.ai.observability.formatters

OSSS.ai.observability.formatters

Logging formatters for structured and correlated logging.

This module provides various formatters for different logging needs, including JSON formatting and correlation ID injection.

JSONFormatter

Bases: Formatter

JSON formatter for structured logging.

Formats log records as JSON with consistent fields including correlation IDs, timestamps, and contextual information.

__init__(include_correlation=True, extra_fields=None)

Initialize JSON formatter.

Parameters

include_correlation : bool Whether to include correlation ID in logs extra_fields : Dict[str, Any], optional Additional fields to include in every log record

format(record)

Format log record as JSON.

CorrelatedFormatter

Bases: Formatter

Human-readable formatter that includes correlation IDs.

Extends the standard formatter to include correlation IDs and observability context in a readable format.

__init__(fmt=None, include_context=True)

Initialize correlated formatter.

Parameters

fmt : str, optional Base format string include_context : bool Whether to include observability context

format(record)

Format log record with correlation information.

get_console_formatter(structured=False, include_correlation=True)

Get appropriate console formatter.

Parameters

structured : bool Whether to use JSON formatting include_correlation : bool Whether to include correlation IDs

Returns

logging.Formatter Configured formatter

get_file_formatter(structured=True, extra_fields=None)

Get appropriate file formatter.

Parameters

structured : bool Whether to use JSON formatting (recommended for files) extra_fields : Dict[str, Any], optional Additional fields to include in logs

Returns

logging.Formatter Configured formatter

get_hostname()

Get hostname for logging.

get_process_id()

Get process ID for logging.

get_python_version()

Get Python version for logging.