Skip to main content

Logger

Struct Logger 

Source
pub struct Logger { /* private fields */ }
Expand description

A file logger that writes messages to path and filters by level.

By default, the logger starts at LogLevel::Info.

Implementations§

Source§

impl Logger

Source

pub fn new<P: Into<PathBuf>>(path: P) -> Self

Creates a new Logger that writes to path.

The default level is LogLevel::Info.

§Examples
use log_easy::Logger;
let logger = Logger::new("app.log");
Examples found in repository?
examples/global.rs (line 13)
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}
More examples
Hide additional examples
examples/instance.rs (line 10)
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}
Source

pub fn with_level(self, level: LogLevel) -> Self

Returns a new Logger configured with the given minimum log level.

Messages below this level are ignored.

§Examples
use log_easy::{Logger, LogLevel};
let logger = Logger::new("app.log").with_level(LogLevel::Debug);
Examples found in repository?
examples/global.rs (line 13)
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}
More examples
Hide additional examples
examples/instance.rs (line 10)
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}
Source

pub fn path(&self) -> &Path

Gets the log file path.

Examples found in repository?
examples/instance.rs (line 29)
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}
Source

pub fn level(&self) -> LogLevel

Gets the current log level.

Examples found in repository?
examples/instance.rs (line 30)
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}
Source

pub fn trace(&self, msg: &str)

These methods log messages at their respective levels. If an error occurs (e.g., file write failure), it is printed to stderr but not returned.

Examples found in repository?
examples/instance.rs (line 13)
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}
Source

pub fn debug(&self, msg: &str)

Examples found in repository?
examples/instance.rs (line 14)
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}
Source

pub fn info(&self, msg: &str)

Examples found in repository?
examples/instance.rs (line 15)
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}
Source

pub fn warn(&self, msg: &str)

Examples found in repository?
examples/instance.rs (line 16)
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}
Source

pub fn error(&self, msg: &str)

Examples found in repository?
examples/instance.rs (line 17)
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}
Source

pub fn try_trace(&self, msg: &str) -> Result<()>

Fallible variants of the logging methods.

Unlike info()/warn() etc., these return std::io::Result<()> so callers can handle failures.

Examples found in repository?
examples/instance.rs (line 20)
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}
Source

pub fn try_debug(&self, msg: &str) -> Result<()>

Examples found in repository?
examples/instance.rs (line 21)
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}
Source

pub fn try_info(&self, msg: &str) -> Result<()>

Examples found in repository?
examples/instance.rs (line 22)
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}
Source

pub fn try_warn(&self, msg: &str) -> Result<()>

Examples found in repository?
examples/instance.rs (line 23)
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}
Source

pub fn try_error(&self, msg: &str) -> Result<()>

Examples found in repository?
examples/instance.rs (line 24)
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}

Trait Implementations§

Source§

impl Debug for Logger

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Logger

§

impl RefUnwindSafe for Logger

§

impl Send for Logger

§

impl Sync for Logger

§

impl Unpin for Logger

§

impl UnwindSafe for Logger

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.