babylon.utils.log
Custom logging utilities for Babylon.
Logging is the nervous system of the simulation. Every action must be traceable, every decision auditable.
This module provides: - JSONFormatter: Dependency-free JSON Lines formatter - TRACE level: Ultra-verbose debugging (level=5) - LogContext: Context propagation for correlation IDs and tick numbers
Module Attributes
Ultra-verbose tracing level for deep debugging. |
Functions
Clear all log context fields. |
|
Get the current log context. |
|
|
Context manager for scoped log context. |
|
Redact sensitive values from a parameters dictionary. |
|
Redact sensitive query parameters from a URL. |
|
Set log context fields. |
Classes
|
Logging filter that injects context fields into LogRecords. |
|
JSON Lines formatter - zero external dependencies. |
- babylon.utils.log.TRACE: int = 5
Ultra-verbose tracing level for deep debugging.
NEVER enable in production. For materialist microscopy only.
- class babylon.utils.log.JSONFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]
Bases:
FormatterJSON Lines formatter - zero external dependencies.
Produces machine-parseable JSONL output suitable for log aggregation. Each log record becomes a single JSON object on one line.
- Output format:
{“ts”:”2025-01-09T14:23:45.123Z”,”level”:”ERROR”,”logger”:”babylon.rag”,…}
Extra fields (tick, correlation_id, exception, etc.) are automatically included from the LogRecord’s __dict__.
- class babylon.utils.log.ContextAwareFilter(name='')[source]
Bases:
FilterLogging filter that injects context fields into LogRecords.
Automatically adds fields from the current log context (tick, simulation_id, correlation_id, etc.) to every log record.
- Usage:
handler.addFilter(ContextAwareFilter())
- babylon.utils.log.set_log_context(**kwargs)[source]
Set log context fields.
These fields will be automatically included in log entries when using the ContextAwareFilter.
- babylon.utils.log.log_context_scope(**kwargs)[source]
Context manager for scoped log context.
Context fields are automatically restored when exiting the scope.
- Usage:
- with log_context_scope(tick=42, simulation_id=”abc”):
logger.info(“Tick complete”) # Includes tick and simulation_id
- babylon.utils.log.redact_url(url)[source]
Redact sensitive query parameters from a URL.
- Parameters:
url (
str) – URL string that may contain API keys in query params.- Return type:
- Returns:
Example
>>> redact_url("https://api.census.gov/data?key=SECRET&get=NAME") 'https://api.census.gov/data?key=***&get=NAME'
- babylon.utils.log.redact_params(params)[source]
Redact sensitive values from a parameters dictionary.
- Parameters:
params (
dict[str,Any]) – Dictionary of parameters that may contain API keys.- Return type:
- Returns:
Example
>>> redact_params({"key": "SECRET", "state": "06"}) {'key': '***', 'state': '06'}