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
impl LoggerBuilder
Sourcepub fn new() -> Self
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?
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}Sourcepub fn level(self, level: LevelFilter) -> Self
pub fn level(self, level: LevelFilter) -> Self
Sets the minimum log level. Returns self for chaining.
Examples found in repository?
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}Sourcepub fn color(self, choice: ColorChoice) -> Self
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?
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}Sourcepub fn source_location(self, source_location: bool) -> Self
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.
Sourcepub fn init(&self) -> Result<(), LoggerError>
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?
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}