pub fn init_logging(
settings: &LoggingSettings,
log_dir: PathBuf,
) -> Result<(), Error>Expand description
Initializes the logging system with JSON output and optional OpenTelemetry export.
This function sets up the complete logging infrastructure:
- Creates the log directory if it doesn’t exist
- Cleans up old log files based on retention policy
- Configures a rolling file appender with non-blocking I/O
- Applies log level filtering from settings or environment
- Sets up JSON formatting for structured logs
- Initializes tracing export if the
tracingfeature is enabled - Bridges
log::records (e.g. from pyroscope-rs) into the tracing pipeline so they appear in Loki alongside tracing events via the tracing-log layer automatically enabled by tracing-subscriber.
The function should only be called once at application startup.
The logging system remains active until shutdown_logging() is called.
§Arguments
settings- Logging configuration including level, directory name, and retention settings.log_dir- Absolute path to the directory where log files are written. The caller is responsible for computing this fromDevice::data_pathso that logs land on the SD card when one is present. PassCURRENT_DEVICE.data_path(&settings.directory)at call sites.
§Returns
Returns Ok(()) on successful initialization.
§Errors
Returns an error if:
- The log directory cannot be created
- Log file cleanup fails
- The rolling file appender cannot be initialized
- The tracing subscriber cannot be initialized
- OpenTelemetry initialization fails (when
otelfeature is enabled)
§Example
use cadmus_core::device::CURRENT_DEVICE;
use cadmus_core::settings::LoggingSettings;
use cadmus_core::logging::init_logging;
let settings = LoggingSettings {
enabled: true,
level: "debug".to_string(),
max_files: 5,
directory: "logs".into(),
otlp_endpoint: Some("http://localhost:4318".to_string()),
pyroscope_endpoint: None,
enable_kern_log: false,
enable_dbus_log: false,
};
let log_dir = CURRENT_DEVICE.data_path(&settings.directory);
init_logging(&settings, log_dir)?;