Core Logging Protocols¶
Logging and Metrics interface for Structum.
This module acts as the Architectural Facade (DP-1) for observability. It provides: 1. Unified API: A consistent interface for logging and metrics regardless of the backend. 2. Operational Continuity: Fallback implementations (Adapters & Null Objects) that ensure the application runs even without plugins. 3. Zero Dependency: Pure stdlib implementations by default.
Plugins (like structum-observability) patch these objects at runtime to inject advanced behavior.
- class structum.logging.StandardLoggerAdapter(logger: Logger)[source]¶
Bases:
objectFallback implementation that adapts standard logging.Logger to LoggerInterface.
Architectural Role: Operational Continuity (DP-2).
This class ensures that calls to log.info(…, user_id=123) do not crash when the advanced observability plugin is missing. It captures structured arguments (**kwargs) and places them into the ‘extra’ dictionary, creating a rudimentary structured logging experience compatible with standard tools.
- structum.logging.get_logger(name: str) LoggerInterface[source]¶
Returns a logger instance conforming to LoggerInterface.
By default, this returns a StandardLoggerAdapter wrapping the stdlib logger. Plugins (like structum_observability) should patch this function to return their own implementation (e.g., a structlog BoundLogger).
- class structum.logging.MetricsCollectorProtocol(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol defining the metrics collection interface.
Implementations should provide methods for incrementing counters and observing values (e.g., for histograms or gauges).
-
increment(metric: str, labels: dict[str, str] | None =
None) None[source]¶ Increment a counter metric.
-
increment(metric: str, labels: dict[str, str] | None =
- class structum.logging.NullMetrics[source]¶
Bases:
objectNull Object implementation for metrics (fallback when no backend is configured).
Architectural Role: Operational Continuity (DP-2).
This class implements the MetricsCollectorProtocol but performs no actions. It allows application code to instrumentation itself (metrics.increment(…)) without checking if a metrics backend is actually installed.
- structum.logging.set_metrics_collector(collector: MetricsCollectorProtocol) None[source]¶
Permette ai plugin di registrare il proprio collettore di metriche.
-
structum.logging.configure_logging(level: str =
'INFO', format: str ='json') None[source]¶ Combines basic logging configuration. Plugins should patch this to provide advanced setup.
- structum.logging.set_context(**kwargs: Any) None[source]¶
Sets global context variables (Fallback: No-op).
In the Core implementation, this is a No-Op. Context propagation requires the structum-observability plugin which implements contextvars management.
- structum.logging.bind_context(**kwargs: Any)[source]¶
Context manager for temporary context (Fallback: yield).
Without the plugin, this simply yields control back to the caller without modifying any context, ensuring code compatibility.