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    // Create an independent Logger instance (separate file/level) without using the global macros.
10    let logger = Logger::new("target/instance.log").with_level(LogLevel::Info);
11
12    // Non-fallible instance logging (write failures go to stderr).
13    logger.trace("This trace message will not be logged due to log level");
14    logger.debug("This debug message will not be logged due to log level");
15    logger.info("Instance logger ready");
16    logger.warn("This is a warning with the instance logger");
17    logger.error("An error occurred with the instance logger");
18
19    // Fallible instance logging: returns Result so you can handle failures.
20    logger.try_trace("This is a trace message with try")?;
21    logger.try_debug("This is a debug message with try")?;
22    logger.try_info("This is an info message with try")?;
23    logger.try_warn("This is a warning with try")?;
24    logger.try_error("An error occurred with try")?;
25
26    // Instance loggers don't have macros, so format messages manually (macros target the global logger).
27    logger.try_info(&format!(
28        "Logger initialized with path: {:?} and level: {:?}",
29        logger.path(),
30        logger.level()
31    ))?;
32
33    Ok(())
34}