rust-loguru 0.1.18

A flexible and extensible logging library for Rust. Similar functionality as python's loguru package.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! A flexible and efficient logging library for Rust.
//!
//! This library provides a powerful logging system with the following features:
//! - Multiple log levels (TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL)
//! - Thread-safe global logger
//! - Extensible handler system
//! - Configurable log formatting
//! - Support for metadata in log records
//! - Convenient logging macros
//! - Asynchronous logging with worker thread pool
//! - Log crate compatibility
//! - Compile-time filtering optimizations
//!
//! # Examples
//!
//! ```rust,no_run
//! use rust_loguru::{Logger, LogLevel, Record};
//! use rust_loguru::handler::NullHandler;
//! use rust_loguru::{info, debug, error};
//! use std::sync::Arc;
//! use parking_lot::RwLock;
//!
//! // Create a logger with a handler
//! let handler = Arc::new(RwLock::new(NullHandler::new(LogLevel::Info)));
//! let mut logger = Logger::new(LogLevel::Debug);
//! logger.add_handler(handler);
//!
//! // Log a message
//! let record = Record::new(
//!     LogLevel::Info,
//!     "Hello, world!",
//!     Some("my_module".to_string()),
//!     Some("main.rs".to_string()),
//!     Some(42),
//! );
//! logger.log(&record);
//!
//! // Or use the convenient macros
//! info!("Hello, world!");
//! debug!("Debug message: {}", 42);
//! error!("Error occurred: {}", "something went wrong");
//!
//! // Asynchronous logging
//! logger.set_async(true, Some(10000));
//! info!("This will be logged asynchronously");
//! ```

pub mod config;
pub mod context;
pub mod error;
pub mod formatter;
pub mod formatters;
pub mod handler;
pub mod integration;
pub mod level;
pub mod logger;
#[doc(hidden)]
pub mod macros;
pub mod record;
pub mod record_pool;
pub mod scope;
pub mod test_utils;

pub use config::{LoggerConfig, LoggerConfigBuilder};
pub use error::{error_chain, install_panic_hook, ContextError, OptionExt, ResultExt};
pub use formatters::json::JsonFormatter;
pub use formatters::template::TemplateFormatter;
pub use formatters::text::TextFormatter;
pub use formatters::FormatterTrait;
pub use handler::Handler;
pub use level::LogLevel;
pub use logger::{global, init, log, Logger};
pub use record::Record;
pub use record_pool::{PooledRecord, RecordPool};
pub use scope::{ScopeError, ScopeGuard};

// Re-export log crate types for compatibility
pub use log::{LevelFilter, Log, Metadata, Record as LogRecord};

// Asynchronous logging types
use crossbeam_channel::{bounded, Receiver, Sender};
use parking_lot::RwLock as PLRwLock;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

/// Compile-time static log level for filtering (can be overridden at build time)
#[allow(dead_code)]
pub static STATIC_LEVEL: level::LogLevel = level::LogLevel::Trace;

/// Async logging command type
#[doc(hidden)]
pub enum AsyncLogCommand {
    /// Log a record
    Log(Record),
    /// Shut down the async logger
    Shutdown,
}

/// Handle to the async logger
#[derive(Clone, Debug)]
pub struct AsyncLoggerHandle {
    /// Channel for sending commands to the async logger
    sender: Sender<AsyncLogCommand>,
    /// Flag indicating whether the async logger is running
    running: Arc<AtomicBool>,
}

impl AsyncLoggerHandle {
    /// Log a record
    pub fn log(&self, record: Record) -> bool {
        if !self.running.load(Ordering::Relaxed) {
            return false;
        }

        self.sender.try_send(AsyncLogCommand::Log(record)).is_ok()
    }

    /// Shut down the async logger
    pub fn shutdown(&self) {
        if !self.running.load(Ordering::Relaxed) {
            return;
        }

        // Send shutdown command and update running flag
        let _ = self.sender.send(AsyncLogCommand::Shutdown);
        self.running.store(false, Ordering::Relaxed);
    }
}

impl Drop for AsyncLoggerHandle {
    fn drop(&mut self) {
        self.shutdown();
    }
}

/// Builder for the async logger
pub struct AsyncLoggerBuilder {
    /// Queue size
    queue_size: usize,
    /// Handler registry
    handlers: Vec<Arc<PLRwLock<dyn Handler>>>,
    /// Log level
    level: LogLevel,
    /// Number of worker threads
    workers: usize,
}

impl AsyncLoggerBuilder {
    /// Create a new async logger builder
    pub fn new() -> Self {
        Self {
            queue_size: 10000,
            handlers: Vec::new(),
            level: LogLevel::Info,
            workers: 1,
        }
    }

    /// Set the queue size
    pub fn with_queue_size(mut self, queue_size: usize) -> Self {
        self.queue_size = queue_size;
        self
    }

    /// Set the handlers
    pub fn with_handlers(mut self, handlers: Vec<Arc<PLRwLock<dyn Handler>>>) -> Self {
        self.handlers = handlers;
        self
    }

    /// Set the log level
    pub fn with_level(mut self, level: LogLevel) -> Self {
        self.level = level;
        self
    }

    /// Set the number of worker threads
    pub fn with_workers(mut self, workers: usize) -> Self {
        self.workers = workers;
        self
    }

    /// Build the async logger
    pub fn build(self) -> AsyncLoggerHandle {
        // Create a channel for sending commands to the worker thread
        let (sender, receiver) = bounded(self.queue_size);

        // Create a running flag
        let running = Arc::new(AtomicBool::new(true));
        let running_clone = running.clone();

        // Spawn the worker threads
        let handlers = self.handlers.clone();
        let level = self.level;

        // Create worker thread pool
        for _ in 0..self.workers {
            let receiver = receiver.clone();
            let handlers = handlers.clone();
            let running = running_clone.clone();

            thread::spawn(move || {
                Self::worker_thread(receiver, handlers, level, running);
            });
        }

        // Create the async logger handle
        AsyncLoggerHandle { sender, running }
    }

    /// Worker thread function
    fn worker_thread(
        receiver: Receiver<AsyncLogCommand>,
        handlers: Vec<Arc<PLRwLock<dyn Handler>>>,
        level: LogLevel,
        running: Arc<AtomicBool>,
    ) {
        while running.load(Ordering::Relaxed) {
            match receiver.recv_timeout(Duration::from_millis(100)) {
                Ok(AsyncLogCommand::Log(record)) => {
                    // Process the record
                    if record.level() >= level {
                        for handler in &handlers {
                            let guard = handler.write();
                            if guard.is_enabled() && record.level() >= guard.level() {
                                let _ = guard.handle(&record);
                            }
                        }
                    }
                }
                Ok(AsyncLogCommand::Shutdown) => {
                    running.store(false, Ordering::Relaxed);
                    break;
                }
                Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                    // Timeout is normal, continue waiting
                    continue;
                }
                Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                    // Channel closed, exit
                    running.store(false, Ordering::Relaxed);
                    break;
                }
            }
        }
    }
}

impl Default for AsyncLoggerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Module for log crate compatibility
pub mod log_adapter {
    use crate::level::LogLevel;
    use crate::logger::global;
    use crate::record::Record as LoguruRecord;
    use log::{Level, Log, Metadata, Record};

    /// Adapter for the log crate
    pub struct LogAdapter;

    impl Log for LogAdapter {
        fn enabled(&self, metadata: &Metadata) -> bool {
            let level = match metadata.level() {
                Level::Error => LogLevel::Error,
                Level::Warn => LogLevel::Warning,
                Level::Info => LogLevel::Info,
                Level::Debug => LogLevel::Debug,
                Level::Trace => LogLevel::Trace,
            };

            level >= global().read().level()
        }

        fn log(&self, record: &Record) {
            if !self.enabled(record.metadata()) {
                return;
            }

            let level = match record.level() {
                Level::Error => LogLevel::Error,
                Level::Warn => LogLevel::Warning,
                Level::Info => LogLevel::Info,
                Level::Debug => LogLevel::Debug,
                Level::Trace => LogLevel::Trace,
            };

            let loguru_record = LoguruRecord::new(
                level,
                record.args().to_string(),
                record.module_path().map(|s| s.to_string()),
                record.file().map(|s| s.to_string()),
                record.line(),
            );

            let _ = global().read().log(&loguru_record);
        }

        fn flush(&self) {
            // Nothing to flush in our implementation
        }
    }

    /// Initialize the log adapter
    pub fn init() -> Result<(), log::SetLoggerError> {
        static LOGGER: LogAdapter = LogAdapter;
        log::set_logger(&LOGGER)?;
        Ok(())
    }

    /// Set the maximum log level for the log crate
    pub fn set_max_level(level: LogLevel) {
        let max_level = match level {
            LogLevel::Error => log::LevelFilter::Error,
            LogLevel::Warning => log::LevelFilter::Warn,
            LogLevel::Info => log::LevelFilter::Info,
            LogLevel::Debug => log::LevelFilter::Debug,
            LogLevel::Trace => log::LevelFilter::Trace,
            _ => log::LevelFilter::Off,
        };

        log::set_max_level(max_level);
    }
}

/// Module for compile-time filtering optimizations
pub mod compile_time {
    use crate::level::LogLevel;

    /// Compile-time level check
    #[macro_export]
    macro_rules! compile_time_level_enabled {
        ($level:expr) => {{
            #[cfg(feature = "max_level_off")]
            {
                false
            }
            #[cfg(not(feature = "max_level_off"))]
            {
                #[cfg(feature = "max_level_error")]
                {
                    $level >= $crate::LogLevel::Error
                }
                #[cfg(feature = "max_level_warn")]
                {
                    $level >= $crate::LogLevel::Warning
                }
                #[cfg(feature = "max_level_info")]
                {
                    $level >= $crate::LogLevel::Info
                }
                #[cfg(feature = "max_level_debug")]
                {
                    $level >= $crate::LogLevel::Debug
                }
                #[cfg(feature = "max_level_trace")]
                {
                    $level >= $crate::LogLevel::Trace
                }
                #[cfg(not(any(
                    feature = "max_level_error",
                    feature = "max_level_warn",
                    feature = "max_level_info",
                    feature = "max_level_debug",
                    feature = "max_level_trace"
                )))]
                {
                    true
                }
            }
        }};
    }

    /// Dynamic runtime level check (used when compile-time check passes)
    #[inline]
    pub fn runtime_level_enabled(level: LogLevel, current_level: LogLevel) -> bool {
        level >= current_level
    }
}

/// Module for benchmarking tools
pub mod benchmark {
    use crate::level::LogLevel;
    use crate::logger::Logger;
    use crate::record::Record;
    use std::time::Instant;

    /// Benchmark logger throughput
    pub fn measure_throughput(logger: &Logger, iterations: usize, level: LogLevel) -> f64 {
        let start = Instant::now();

        for i in 0..iterations {
            let record = Record::new(
                level,
                format!("Benchmark message {}", i),
                Some("benchmark".to_string()),
                Some("benchmark.rs".to_string()),
                Some(1),
            );
            let _ = logger.log(&record);
        }

        let elapsed = start.elapsed();
        iterations as f64 / elapsed.as_secs_f64()
    }

    /// Compare sync vs async performance
    pub fn compare_sync_vs_async(
        logger: &mut Logger,
        iterations: usize,
        level: LogLevel,
    ) -> (f64, f64) {
        // Measure sync throughput
        let sync_throughput = measure_throughput(logger, iterations, level);

        // Enable async logging
        logger.set_async(true, Some(iterations));

        // Measure async throughput
        let async_throughput = measure_throughput(logger, iterations, level);

        // Disable async logging
        logger.set_async(false, None);

        (sync_throughput, async_throughput)
    }
}