dbx_core/logging.rs
1//! Logging utilities for DBX
2//!
3//! Provides helpers for initializing tracing subscribers.
4
5#[cfg(feature = "logging")]
6use tracing_subscriber::{EnvFilter, fmt};
7
8/// Initialize logging with default settings
9///
10/// # Environment Variables
11/// - `RUST_LOG` - Log level filter (default: "info")
12///
13/// # Example
14/// ```rust
15/// dbx_core::logging::init();
16/// ```
17#[cfg(feature = "logging")]
18pub fn init() {
19 init_with_level("info")
20}
21
22/// Initialize logging with a specific level
23///
24/// # Arguments
25/// * `level` - Log level (trace, debug, info, warn, error)
26///
27/// # Example
28/// ```rust
29/// dbx_core::logging::init_with_level("debug");
30/// ```
31#[cfg(feature = "logging")]
32pub fn init_with_level(level: &str) {
33 let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(level));
34
35 fmt()
36 .with_env_filter(filter)
37 .with_target(true)
38 .with_thread_ids(true)
39 .with_line_number(true)
40 .init();
41}
42
43/// Initialize logging for tests
44///
45/// Uses a more verbose format suitable for debugging tests.
46#[cfg(feature = "logging")]
47pub fn init_test() {
48 let _ = fmt()
49 .with_env_filter(EnvFilter::new("debug"))
50 .with_test_writer()
51 .try_init();
52}
53
54// Stub implementations when logging feature is disabled
55#[cfg(not(feature = "logging"))]
56pub fn init() {}
57
58#[cfg(not(feature = "logging"))]
59pub fn init_with_level(_level: &str) {}
60
61#[cfg(not(feature = "logging"))]
62pub fn init_test() {}