init_logging

Function init_logging 

Source
pub fn init_logging(settings: &LoggingSettings) -> 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 OpenTelemetry export if the otel feature is enabled

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, and retention

§Returns

Returns Ok(()) on successful initialization.

§Errors

Returns an error if:

  • The current working directory cannot be determined
  • The log directory cannot be created
  • Log file cleanup fails
  • The rolling file appender cannot be initialized
  • The log filter configuration is invalid
  • The tracing subscriber cannot be initialized
  • OpenTelemetry initialization fails (when otel feature is enabled)

§Example

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()),
};

init_logging(&settings)?;