Skip to main content

LoggerBuilder

Struct LoggerBuilder 

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

Configures and initializes the global logger, which writes elapsed time, colored with each level’s color when stderr supports it, on top of env_logger.

LoggerBuilder uses a fluent builder pattern. Construct it via new, configure it with level, color and source_location, then initialize it with init. Each line is written to stderr as described in Format.

§Example

use caesium::{info, LoggerBuilder, LevelFilter};

LoggerBuilder::new()
    .level(LevelFilter::Debug)
    .init()
    .expect("failed to init logger");

info!("Application started");

Implementations§

Source§

impl LoggerBuilder

Source

pub fn new() -> Self

Creates a builder with the default level of LevelFilter::Info, color set to ColorChoice::Auto and source location shown.

Examples found in repository?
examples/main.rs (line 7)
6fn main() {
7    let builder = LoggerBuilder::new()
8        .level(LevelFilter::Debug)
9        .color(ColorChoice::Auto);
10
11    builder.init().expect("logger init failed");
12
13    info!("Logger initialized successfully!");
14    debug!("This is a debug message");
15    trace!("This is a trace message");
16    warn!("This is a warning message");
17    error!("This is an error message");
18
19    // Simulate some work to demonstrate elapsed time tracking
20    std::thread::sleep(std::time::Duration::from_millis(150));
21    info!("After a short delay");
22
23    std::thread::sleep(std::time::Duration::from_millis(250));
24    warn!("Another warning after more time has passed");
25
26    // Calling init again fails: a global logger is already initialized
27    match builder.init() {
28        Err(e) => println!("Expected init error: {e}"),
29        Ok(()) => unreachable!("init should not succeed a second time"),
30    }
31}
Source

pub fn level(self, level: LevelFilter) -> Self

Sets the minimum log level. Returns self for chaining.

Examples found in repository?
examples/main.rs (line 8)
6fn main() {
7    let builder = LoggerBuilder::new()
8        .level(LevelFilter::Debug)
9        .color(ColorChoice::Auto);
10
11    builder.init().expect("logger init failed");
12
13    info!("Logger initialized successfully!");
14    debug!("This is a debug message");
15    trace!("This is a trace message");
16    warn!("This is a warning message");
17    error!("This is an error message");
18
19    // Simulate some work to demonstrate elapsed time tracking
20    std::thread::sleep(std::time::Duration::from_millis(150));
21    info!("After a short delay");
22
23    std::thread::sleep(std::time::Duration::from_millis(250));
24    warn!("Another warning after more time has passed");
25
26    // Calling init again fails: a global logger is already initialized
27    match builder.init() {
28        Err(e) => println!("Expected init error: {e}"),
29        Ok(()) => unreachable!("init should not succeed a second time"),
30    }
31}
Source

pub fn color(self, choice: ColorChoice) -> Self

Sets when lines are colored with their level’s color. Defaults to ColorChoice::Auto. Returns self for chaining.

Examples found in repository?
examples/main.rs (line 9)
6fn main() {
7    let builder = LoggerBuilder::new()
8        .level(LevelFilter::Debug)
9        .color(ColorChoice::Auto);
10
11    builder.init().expect("logger init failed");
12
13    info!("Logger initialized successfully!");
14    debug!("This is a debug message");
15    trace!("This is a trace message");
16    warn!("This is a warning message");
17    error!("This is an error message");
18
19    // Simulate some work to demonstrate elapsed time tracking
20    std::thread::sleep(std::time::Duration::from_millis(150));
21    info!("After a short delay");
22
23    std::thread::sleep(std::time::Duration::from_millis(250));
24    warn!("Another warning after more time has passed");
25
26    // Calling init again fails: a global logger is already initialized
27    match builder.init() {
28        Err(e) => println!("Expected init error: {e}"),
29        Ok(()) => unreachable!("init should not succeed a second time"),
30    }
31}
Source

pub fn source_location(self, source_location: bool) -> Self

Shows or hides the source location (file:line) on each line. Shown by default. Returns self for chaining.

Source

pub fn init(&self) -> Result<(), LoggerError>

Initializes the global logger, which writes lines as described in Format. Elapsed time on each line is measured from the moment this method runs.

§Errors

Only one global logger can exist per process. If one is already initialized, whether by an earlier call to this method or by another crate, this returns LoggerError::AlreadyInitialized and changes nothing.

Examples found in repository?
examples/main.rs (line 11)
6fn main() {
7    let builder = LoggerBuilder::new()
8        .level(LevelFilter::Debug)
9        .color(ColorChoice::Auto);
10
11    builder.init().expect("logger init failed");
12
13    info!("Logger initialized successfully!");
14    debug!("This is a debug message");
15    trace!("This is a trace message");
16    warn!("This is a warning message");
17    error!("This is an error message");
18
19    // Simulate some work to demonstrate elapsed time tracking
20    std::thread::sleep(std::time::Duration::from_millis(150));
21    info!("After a short delay");
22
23    std::thread::sleep(std::time::Duration::from_millis(250));
24    warn!("Another warning after more time has passed");
25
26    // Calling init again fails: a global logger is already initialized
27    match builder.init() {
28        Err(e) => println!("Expected init error: {e}"),
29        Ok(()) => unreachable!("init should not succeed a second time"),
30    }
31}

Trait Implementations§

Source§

impl Debug for LoggerBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for LoggerBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.