Skip to main content

global/
global.rs

1// examples/global.rs
2// Demonstrates the global logger + macros (info!, try_info!, etc).
3// Run: cargo run --example global
4
5use log_easy::{
6    LogLevel, Logger, debug, error, info, trace, try_debug, try_error, try_info, try_trace,
7    try_warn, warn,
8};
9use std::io::Result;
10
11fn main() -> Result<()> {
12    // Initialize the global logger with a file path and log level.
13    log_easy::init_with(Logger::new("target/global.log").with_level(LogLevel::Trace))?;
14
15    // Non-fallible macros (errors are printed to stderr).
16    trace!("This is a trace message");
17    debug!("Debugging information here");
18    info!("Application started");
19    warn!("This is a warning");
20    error!("An error occurred");
21
22    // Fallible macros (return Result so you can handle failures).
23    try_trace!("This is a trace message using the try macro")?;
24    try_debug!("This is a debug message using the try macro")?;
25    try_info!("This is an info message using the try macro")?;
26    try_warn!("This is a warning message using the try macro")?;
27    try_error!("This is an error message using the try macro")?;
28
29    // Macros accept formatting args.
30    let user = "joe";
31    let attempts = 3;
32    let elapsed_ms = 127;
33    info!("User {user} has attempted login {attempts} times ({elapsed_ms}ms elapsed)");
34
35    Ok(())
36}