manager.logger

manager.logger

Audit-grade logging for spotforecast2-safe.

This module sets up a dual-handler logger: a console handler for humans and a file handler that writes one JSON object per line, conforming to the schema at audit_log_schema.json next to this file. The file is the compliance-relevant sink (EU AI Act Article 12, IEC 62443-4-2 SAR 6.1, IEC 61508-3 §7.4.7); the console handler is plaintext for interactive use.

Schema versioning rule: SCHEMA_VERSION is loaded directly from audit_log_schema.json at import time, so there is exactly one source of truth. Any change to the schema file is guarded by the CI job audit-log-schema-gate (.github/workflows/ci.yml), which rejects a pull request that touches the schema without a Conventional-Commits breaking-change marker (type!:) on at least one commit. That marker cascades to a MAJOR semantic-release bump.

Classes

Name Description
JsonAuditFormatter Format LogRecord instances as single-line JSON per audit_log_schema.json.

JsonAuditFormatter

manager.logger.JsonAuditFormatter()

Format LogRecord instances as single-line JSON per audit_log_schema.json.

The formatter emits exactly the fields named in the schema’s properties section, never more. Callers pass optional structured context through the standard logging extra= mechanism; recognised extras are event, task, and context.

Functions

Name Description
setup_logging Configure dual-handler logging for safety-critical execution.

setup_logging

manager.logger.setup_logging(level=logging.INFO, log_dir=None)

Configure dual-handler logging for safety-critical execution.

Attaches a stream handler (stdout, human-readable plaintext) and, when log_dir is provided, a file handler that writes JSON records per audit_log_schema.json. The console handler honours level; the file handler is always at INFO so the audit trail stays complete even when the operator silences the console.

Parameters

Name Type Description Default
level int Logging level for console output. Default: logging.INFO. logging.INFO
log_dir Optional[Path] Optional directory for the audit log file. If provided, a timestamped task_safe_n_to_1_YYYYMMDD_HHMMSS.log file is created and receives JSON-formatted records. None

Returns

Name Type Description
logging.Logger Tuple of the configured logger and the audit log file path (or
Optional[Path] None if log_dir was omitted).