Expand description
Logging utilities and macros for MoosicBox services.
This crate provides optional integration with free_log_client for structured
logging initialization, as well as convenience macros and re-exports for
runtime log emission.
§MoosicBox Logging
Logging utilities with feature-gated modules for MoosicBox applications.
§Overview
The MoosicBox Logging package provides:
- Free Log Integration: Initialize and configure
free_log_clientfor structured logging - Logging Macros: Conditional logging macros (e.g.,
debug_or_trace!) - API Support: Optional API feature for
free_log_client - Feature-Gated Modules: Enable only the logging components you need
§Current Implementation
§Core Components
- Free Log Module: Provides
init()function to configurefree_log_clientwith file writing and custom layers - Macro Module: Provides
debug_or_trace!macro for conditional logging based on log level - Re-exports: Exposes
free_log_clientandlogcrates for convenience
§Available Features
api: Enables API features infree_log_client(enabled by default)free_log: Enablesfree_logintegration module with init function (enabled by default)macros: Enables logging macro utilities (enabled by default)
§Installation
§Cargo Dependencies
[dependencies]
# With default features (api, free_log, macros)
moosicbox_logging = { path = "../logging" }
# Disable default features and enable specific ones
moosicbox_logging = {
path = "../logging",
default-features = false,
features = ["free_log"]
}§Usage
§Initializing Free Log
use moosicbox_logging::{init, InitError};
fn setup_logging() -> Result<(), InitError> {
// Initialize with a log file
let _layer = init(Some("app.log"), None)?;
// Or initialize without a file
let _layer = init(None, None)?;
// Or initialize with custom layers
use moosicbox_logging::free_log_client::DynLayer;
let custom_layers: Vec<DynLayer> = vec![/* your layers */];
let _layer = init(Some("app.log"), Some(custom_layers))?;
Ok(())
}The init function:
- Configures environment-based log filtering (
MOOSICBOX_LOGorRUST_LOGenvironment variables) - Sets default log level to
tracein debug builds,infoin release builds - Optionally writes logs to a file in the config directory’s
logssubdirectory - Supports custom tracing layers
§Using Logging Macros
use moosicbox_logging::log;
use moosicbox_logging::debug_or_trace;
fn example() {
// Standard log macros (re-exported from `log` crate)
log::info!("Application started");
log::debug!("Debug information");
// Conditional macro: logs at trace level if enabled, otherwise debug
debug_or_trace!(
("Short debug message"),
("Detailed trace message with extra context")
);
}§Implementation Notes
- All features (
api,free_log,macros) are enabled by default - Without any features enabled, the package provides no functionality (empty lib)
- The
free_logfeature requiresmoosicbox_configandmoosicbox_env_utilsdependencies - The
apifeature enables API functionality in the underlyingfree_log_client - Log files are written to
{config_dir}/logs/{filename}when a filename is provided - Features can be selectively disabled if not needed
§Features
- Default: Includes
api,free_log, andmacrosfeatures api: Enables API support infree_log_clientfree_log: Enablesfree_logintegration module withinit()functionmacros: Enables logging macro utilities (debug_or_trace!macro)
§API Reference
§Free Log Module (feature = “free_log”)
§init Function
use moosicbox_logging::free_log_client::{DynLayer, FreeLogLayer};
use moosicbox_logging::InitError;
pub fn init(
filename: Option<&str>,
layers: Option<Vec<DynLayer>>,
) -> Result<FreeLogLayer, InitError>Initializes the logging system with optional file output and custom layers.
Parameters:
filename: Optional log file name (written to{config_dir}/logs/{filename})layers: Optional vector of custom tracing layers
Returns: Result<FreeLogLayer, InitError>
Errors:
InitError::Logs: Failed to initialize logsInitError::BuildLogsConfig: Failed to build logs configInitError::BuildFileWriterConfig: Failed to build file writer config
§Re-exports
pub use free_log_client;- Exposes the entirefree_log_clientcrate
§Macros Module (feature = “macros”)
§debug_or_trace! Macro
// Conditionally logs at trace level if enabled, otherwise logs at debug level
debug_or_trace!(
("Debug message"),
("Trace message")
);Conditionally logs at trace level if enabled, otherwise logs at debug level.
§Re-exports
pub use log;- Exposes the standardlogcrate
§Dependencies
§Core Dependencies (always included)
free_log_client: Free log client for structured logginglog: Standard Rust logging facadethiserror: Error handling
§Feature-Specific Dependencies
moosicbox_config(whenfree_logis enabled): Config directory utilitiesmoosicbox_env_utils(whenfree_logis enabled): Environment variable helpers
§Package Structure
src/
├── lib.rs # Feature-gated module exports
├── free_log.rs # Free log initialization (feature = "free_log")
└── macros.rs # Logging macros (feature = "macros")Re-exports§
pub use free_log_client;pub use log;
Macros§
- debug_
or_ trace - Logs a message at trace level if trace logging is enabled, otherwise at debug level.
Enums§
- Init
Error - Error type for logging initialization failures.
Functions§
- init
- Initializes the logging system with optional file output and custom layers.