zlogger/
lib.rs

1//! # zlogger
2//! 
3//! ## Features
4//! 
5//! - Color-coded log levels for terminal output
6//! - Environment variable configuration
7//! - File logging with automatic rotation
8//! - Multiple output targets (console, file, or both)
9//! - Customizable log formats
10//! - Thread-safe logging
11//! 
12//! ## Quick Start
13//! 
14//! ```rust
15//! use colorful_logger::{init, info, warn, error};
16//! 
17//! // Initialize with default settings
18//! init();
19//! 
20//! // Log messages
21//! info!("Application started");
22//! warn!("This is a warning");
23//! error!("Something went wrong");
24//! ```
25//! 
26//! ## Environment Variables
27//! 
28//! - `ZLOG_LEVEL`: Set log level (trace, debug, info, warn, error)
29//! - `ZLOG_OUTPUT`: Set output target (console, file, both)
30//! - `ZLOG_FILE`: Set log file path
31//! - `ZLOG_MAX_SIZE`: Set max file size before rotation (in bytes)
32//! - `ZLOG_MAX_FILES`: Set maximum number of rotated files to keep
33//! - `ZLOG_COLOR`: Enable/disable colors (true/false)
34
35pub mod logger;
36pub mod config;
37pub mod formatter;
38pub mod rotation;
39pub mod colors;
40
41pub use logger::Logger;
42pub use config::{Config, LogLevel, OutputTarget};
43
44use std::sync::Once;
45
46static INIT: Once = Once::new();
47
48/// Initialize the global logger with default configuration
49pub fn init() {
50    INIT.call_once(|| {
51        let config = Config::from_env();
52        Logger::init(config).expect("Failed to initialize logger");
53    });
54}
55
56/// Initialize the global logger with custom configuration
57pub fn init_with_config(config: Config) {
58    INIT.call_once(|| {
59        Logger::init(config).expect("Failed to initialize logger");
60    });
61}
62
63/// Log a trace message
64#[macro_export]
65macro_rules! trace {
66    ($($arg:tt)*) => {
67        $crate::logger::log($crate::LogLevel::Trace, format_args!($($arg)*))
68    };
69}
70
71/// Log a debug message
72#[macro_export]
73macro_rules! debug {
74    ($($arg:tt)*) => {
75        $crate::logger::log($crate::LogLevel::Debug, format_args!($($arg)*))
76    };
77}
78
79/// Log an info message
80#[macro_export]
81macro_rules! info {
82    ($($arg:tt)*) => {
83        $crate::logger::log($crate::LogLevel::Info, format_args!($($arg)*))
84    };
85}
86
87/// Log a warning message
88#[macro_export]
89macro_rules! warn {
90    ($($arg:tt)*) => {
91        $crate::logger::log($crate::LogLevel::Warn, format_args!($($arg)*))
92    };
93}
94
95/// Log an error message
96#[macro_export]
97macro_rules! error {
98    ($($arg:tt)*) => {
99        $crate::logger::log($crate::LogLevel::Error, format_args!($($arg)*))
100    };
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_macros() {
109        init();
110        trace!("This is a trace message");
111        debug!("This is a debug message");
112        info!("This is an info message");
113        warn!("This is a warning message");
114        error!("This is an error message");
115    }
116}