rust_loguru/integration.rs
1//! Integration module for rust-loguru
2//!
3//! - Log crate compatibility
4//! - Async runtime integrations
5//! - Framework middleware
6
7/// Integration with the `log` crate.
8pub mod log_compat {
9 /// Initialize rust-loguru as the global logger for the `log` crate.
10 pub fn init_loguru_as_log() {
11 // Use the log_adapter module from the crate root
12 crate::log_adapter::init().expect("Failed to set log adapter as global logger");
13 let level = crate::logger::global().read().level();
14 crate::log_adapter::set_max_level(level);
15 }
16}
17
18/// Async runtime integrations (e.g., tokio).
19pub mod async_runtime {
20 /// Integrate with async runtimes (e.g., spawn log flushing tasks).
21 #[cfg(feature = "tokio")]
22 pub async fn integrate_with_tokio() {
23 use crate::logger::global;
24 use std::sync::Arc;
25 use tokio::time::{sleep, Duration};
26
27 // Example: spawn a background task that could flush logs every 100ms
28 tokio::spawn(async move {
29 loop {
30 // In a real implementation, you might flush async log queues here
31 // For now, just sleep to demonstrate integration
32 sleep(Duration::from_millis(100)).await;
33 // Optionally, call a flush method if implemented
34 let _ = global();
35 }
36 });
37 }
38
39 #[cfg(not(feature = "tokio"))]
40 pub async fn integrate_with_tokio() {
41 panic!("tokio integration requires the 'tokio' feature to be enabled");
42 }
43}
44
45/// Framework middleware (e.g., for web frameworks).
46pub mod middleware {
47 /// Middleware for web frameworks (e.g., actix, axum).
48 pub fn request_response_logging() {
49 // TODO: Implement request/response logging middleware
50 unimplemented!("framework middleware not yet implemented");
51 }
52}