brokkr-utils::logging
Rust
Brokkr Logging Module
This module provides a custom logging framework for the Brokkr application.
Features
- Thread-safe logging
- Dynamic log level adjustment
- Custom log macros for easy use
Usage
-
Initialize the logger:
brokkr_logger::init("info").expect("Failed to initialize logger"); -
Use the log macros throughout your code:
debug!("This is a debug message");info!("This is an info message");warn!("This is a warning message");error!("This is an error message"); -
Update log level at runtime if needed:
brokkr_logger::update_log_level("debug").expect("Failed to update log level");
Log Levels
The available log levels are:
- “off”: Turn off all logging
- “error”: Log only errors
- “warn”: Log warnings and errors
- “info”: Log info, warnings, and errors (default)
- “debug”: Log debug messages and all above
- “trace”: Log trace messages and all above
Thread Safety
This logger is designed to be thread-safe and can handle concurrent logging operations and log level changes from multiple threads.
Re-exports
Section titled “Re-exports”debug(log::debug)error(log::error)info(log::info)trace(log::trace)warn(log::warn)
Structs
Section titled “Structs”struct BrokkrLogger
Section titled “struct BrokkrLogger”pub
Custom logger for the Brokkr application
Functions
Section titled “Functions”fn init
Section titled “fn init”pub
fn init(level: &str) -> Result<(), SetLoggerError>Initializes the Brokkr logging system with the specified log level.
Sets up a custom logger that handles structured logging with timestamps, log levels, and module paths. Supports multiple output formats and concurrent logging from multiple threads.
Features
- Thread-safe logging
- Configurable log levels
- Module path tracking
- Timestamp inclusion
- Error context preservation
Error Cases
- Invalid log level string
- Logger already initialized
- System permission issues
Parameters:
| Name | Type | Description |
|---|---|---|
level |
&str |
String representation of the log level (“debug”, “info”, “warn”, “error”) |
Returns:
Result<(), Box<dyn Error>>- Success/failure of logger initialization
Examples:
use brokkr_utils::logging;
logging::init("debug")?;log::info!("Logger initialized successfully");Source
pub fn init(level: &str) -> Result<(), SetLoggerError> { init_with_format(level, "text")}fn init_with_format
Section titled “fn init_with_format”pub
fn init_with_format( level: &str) -> Result<(, format: &str) -> Result<(,) -> Result<(), SetLoggerError>Initializes the Brokkr logging system with the specified log level and format.
Parameters:
| Name | Type | Description |
|---|---|---|
level |
&str |
String representation of the log level (“debug”, “info”, “warn”, “error”) |
format |
&str |
Log output format (“text” for human-readable, “json” for structured JSON) |
Returns:
Result<(), SetLoggerError>- Success/failure of logger initialization
Source
pub fn init_with_format(level: &str, format: &str) -> Result<(), SetLoggerError> { let level_filter = str_to_level_filter(level); let use_json = format.eq_ignore_ascii_case("json");
INIT.get_or_init(|| { log::set_logger(&LOGGER) .map(|()| log::set_max_level(LevelFilter::Trace)) .expect("Failed to set logger"); });
JSON_FORMAT.store(use_json, Ordering::Relaxed); CURRENT_LEVEL.store(level_filter as usize, Ordering::Relaxed); log::set_max_level(level_filter); Ok(())}fn update_log_level
Section titled “fn update_log_level”pub
fn update_log_level(level: &str) -> Result<(), String>Updates the current log level.
Parameters:
| Name | Type | Description |
|---|---|---|
level |
&str |
A string slice that holds the new desired log level. |
Returns:
Ok(())if the log level was successfully updated.Err(String)if there was an error updating the log level.
Examples:
use brokkr_logger;
brokkr_logger::init("info").expect("Failed to initialize logger"); info!("This will be logged");
brokkr_logger::update_log_level("warn").expect("Failed to update log level"); info!("This will not be logged"); warn!("But this will be logged");Source
pub fn update_log_level(level: &str) -> Result<(), String> { let new_level = str_to_level_filter(level); CURRENT_LEVEL.store(new_level as usize, Ordering::Relaxed); log::set_max_level(new_level); Ok(())}