stdout_only/
stdout_only.rs

1// This example demonstrates how to set up a logger that only outputs to stdout,
2// with customized formatting for cleaner console output.
3//
4// Key features shown:
5// - Using LoggerConfig builder pattern
6// - Configuring a stdout-only logger (no log file)
7// - Hiding file/line information for cleaner logs
8// - Setting the minimum log level
9
10use fstdout_logger::{LoggerConfig, init_stdout_logger};
11use log::{LevelFilter, debug, error, info, trace, warn};
12
13fn main() {
14    // Initialize logger with stdout only (no file output) with colors
15    let config = LoggerConfig::builder()
16        .level(LevelFilter::Debug) // Show Debug level and above
17        .show_file_info(false) // Don't show file and line information
18        .use_colors(true) // Use colors in output
19        .build();
20
21    if let Err(e) = init_stdout_logger(config) {
22        eprintln!("Failed to initialize logger: {e}");
23        return;
24    }
25
26    println!("Logger initialized! Output will only appear on stdout.");
27    println!("Notice that TRACE level messages won't show because we set Debug as minimum level.");
28    println!("Also note that file and line information is hidden for cleaner output.");
29
30    // Log messages at different levels to demonstrate filtering
31    trace!("This is a TRACE message - you won't see this");
32    debug!("This is a DEBUG message - visible");
33    info!("This is an INFO message - visible");
34    warn!("This is a WARNING message - visible");
35    error!("This is an ERROR message - visible");
36
37    // Log messages with dynamic content
38    for i in 1..=3 {
39        let value = i * 10;
40        debug!("Debug calculation: {i} * 10 = {value}");
41        info!("Processing item #{i} with value {value}");
42    }
43
44    info!("Example completed");
45}