Expand description
Structured logging infrastructure with JSON output and OpenTelemetry integration.
This module provides logging functionality for Cadmus, including:
- JSON-structured logs written to rotating files
- Configurable log levels and filtering
- Automatic log file cleanup based on retention policies
- Optional OpenTelemetry export (when
otelfeature is enabled) - Unique run ID for correlating logs across a session
§Architecture
The logging system is built on the tracing crate ecosystem:
tracing_subscriberfor composable logging layerstracing_appenderfor non-blocking file I/O- JSON formatting for structured, machine-readable logs
EnvFilterfor flexible log level control
Each application run generates a unique Run ID (UUID v7) that appears in:
- The log filename:
cadmus-<run_id>.json - OpenTelemetry resource attributes
- All log entries for correlation
§Log File Management
Log files are automatically managed:
- Files are named with the run ID:
cadmus-<run_id>.json - Older files are deleted when
max_fileslimit is exceeded - Cleanup happens at initialization, keeping only the most recent files
§Configuration
Logging is configured via LoggingSettings:
[logging]
enabled = true
level = "info"
max-files = 3
directory = "logs"
otlp-endpoint = "http://localhost:4318" # OptionalThe log level can be overridden with the RUST_LOG environment variable:
RUST_LOG=debug ./cadmus
RUST_LOG=cadmus::view=trace,info ./cadmus§Example Usage
use cadmus_core::settings::LoggingSettings;
use cadmus_core::logging::{init_logging, shutdown_logging, get_run_id};
let settings = LoggingSettings {
enabled: true,
level: "info".to_string(),
max_files: 3,
directory: "logs".into(),
otlp_endpoint: None,
};
// Initialize at application startup
init_logging(&settings)?;
eprintln!("Started with run ID: {}", get_run_id());
// Use tracing macros throughout the application
tracing::info!("Application started");
// Shutdown at application exit (flushes buffers)
shutdown_logging();Constants§
Statics§
Functions§
- build_
filter 🔒 - Builds an
EnvFilterfrom settings or environment variables. - cleanup_
run_ 🔒logs - Removes old log files to maintain the configured retention limit.
- collect_
run_ 🔒log_ entries - Collects all Cadmus log file entries from the specified directory.
- get_
run_ id - Returns the unique run ID for this application session.
- init_
logging - Initializes the logging system with JSON output and optional OpenTelemetry export.
- is_
run_ 🔒log_ entry - Determines whether a directory entry is a Cadmus log file.
- shutdown_
logging - Gracefully shuts down the logging system and flushes buffered data.