Skip to main content

instance/
instance.rs

1// examples/instance.rs
2// Demonstrates using a standalone Logger instance (no global macros).
3// Run: cargo run --example instance
4
5use log_easy::{LogLevel, Logger};
6use std::io::Result;
7
8fn main() -> Result<()> {
9    // Parse a string into LogLevel enum.
10    let level: LogLevel = "Debug".parse().expect("valid hard-coded log level");
11
12    // Create an independent Logger instance (separate file/level) without using the global macros.
13    let logger = Logger::new("target/instance.log").with_level(level);
14
15    // Non-fallible instance logging (write failures go to stderr).
16    logger.trace("This trace message will not be logged due to log level");
17    logger.debug("This debug message will not be logged due to log level");
18    logger.info("Instance logger ready");
19    logger.warn("This is a warning with the instance logger");
20    logger.error("An error occurred with the instance logger");
21
22    // Fallible instance logging: returns Result so you can handle failures.
23    logger.try_trace("This is a trace message with try")?;
24    logger.try_debug("This is a debug message with try")?;
25    logger.try_info("This is an info message with try")?;
26    logger.try_warn("This is a warning with try")?;
27    logger.try_error("An error occurred with try")?;
28
29    // Instance loggers don't have macros, so format messages manually (macros target the global logger).
30    logger.try_info(&format!(
31        "Logger initialized with path: {:?} and level: {:?}",
32        logger.path(),
33        logger.level()
34    ))?;
35
36    Ok(())
37}