// std/log - Logging with proper abstraction
// Implementation: env_logger / tracing (wrapped for clean API)
// PUBLIC API - Users interact with these functions only
//===============================================
// LOG LEVELS
//===============================================
// Initialize logger (call once at start of program)
fn init() {
// Wraps: env_logger::init() or tracing_subscriber::fmt::init()
// Sets up logging based on RUST_LOG environment variable
}
// Initialize with custom filter level
fn init_with_level(level: string) {
// Wraps: env_logger::Builder::new().filter_level(level).init()
// Levels: "trace", "debug", "info", "warn", "error"
}
//===============================================
// LOGGING FUNCTIONS
//===============================================
// Trace level - very detailed debugging
fn trace(message: string) {
// Wraps: log::trace!("{}", message)
println!("[TRACE] {}", message)
}
// Debug level - debugging information
fn debug(message: string) {
// Wraps: log::debug!("{}", message)
println!("[DEBUG] {}", message)
}
// Info level - general information
fn info(message: string) {
// Wraps: log::info!("{}", message)
println!("[INFO] {}", message)
}
// Warn level - warning messages
fn warn(message: string) {
// Wraps: log::warn!("{}", message)
println!("[WARN] {}", message)
}
// Error level - error messages
fn error(message: string) {
// Wraps: log::error!("{}", message)
println!("[ERROR] {}", message)
}
//===============================================
// STRUCTURED LOGGING (with key-value pairs)
//===============================================
// Trace with context
fn trace_with(message: string, key: string, value: string) {
// Wraps: log::trace!("{} - {}: {}", message, key, value)
println!("[TRACE] {} - {}: {}", message, key, value)
}
// Debug with context
fn debug_with(message: string, key: string, value: string) {
// Wraps: log::debug!("{} - {}: {}", message, key, value)
println!("[DEBUG] {} - {}: {}", message, key, value)
}
// Info with context
fn info_with(message: string, key: string, value: string) {
// Wraps: log::info!("{} - {}: {}", message, key, value)
println!("[INFO] {} - {}: {}", message, key, value)
}
// Warn with context
fn warn_with(message: string, key: string, value: string) {
// Wraps: log::warn!("{} - {}: {}", message, key, value)
println!("[WARN] {} - {}: {}", message, key, value)
}
// Error with context
fn error_with(message: string, key: string, value: string) {
// Wraps: log::error!("{} - {}: {}", message, key, value)
println!("[ERROR] {} - {}: {}", message, key, value)
}
//===============================================
// LOG LEVEL CHECKING
//===============================================
// Check if trace logging is enabled
fn trace_enabled() -> bool {
// Wraps: log::log_enabled!(log::Level::Trace)
false
}
// Check if debug logging is enabled
fn debug_enabled() -> bool {
// Wraps: log::log_enabled!(log::Level::Debug)
false
}
// Check if info logging is enabled
fn info_enabled() -> bool {
// Wraps: log::log_enabled!(log::Level::Info)
true
}
// Check if warn logging is enabled
fn warn_enabled() -> bool {
// Wraps: log::log_enabled!(log::Level::Warn)
true
}
// Check if error logging is enabled
fn error_enabled() -> bool {
// Wraps: log::log_enabled!(log::Level::Error)
true
}
// USAGE EXAMPLES:
//
// use std::log
//
// fn main() {
// // Initialize logger (reads RUST_LOG env var)
// log.init()
//
// // Or initialize with specific level
// log.init_with_level("debug")
//
// // Simple logging
// log.trace("Starting application")
// log.debug("Loading configuration")
// log.info("Server started on port 3000")
// log.warn("Connection pool running low")
// log.error("Failed to connect to database")
//
// // Structured logging with context
// log.info_with("User logged in", "user_id", "12345")
// log.warn_with("Slow query detected", "duration_ms", "1500")
// log.error_with("Request failed", "status_code", "500")
//
// // Check if logging is enabled before expensive operations
// if log.debug_enabled() {
// let debug_info = expensive_debug_calculation()
// log.debug(debug_info)
// }
// }
//
// ENVIRONMENT VARIABLE:
// Set log level via RUST_LOG:
// RUST_LOG=debug cargo run # Show debug and above
// RUST_LOG=info cargo run # Show info and above (default)
// RUST_LOG=warn cargo run # Show warn and error only
// RUST_LOG=myapp=trace cargo run # Trace for myapp, default for others
//
// NOT THIS (log crate exposed): ❌
// log::info!("Message")
// env_logger::init()
//
// NOTE: env_logger and log crates are auto-added as dependencies