The log
crate is the de-facto logging API in Rust.
One important note is that the log
crate only provides the API, not implementations.
log
crate takes an environment variable RUST_LOG
to configure its log level: std::env::set_var("RUST_LOG", "debug");
.
There are 5 log levels:
- error (highest priority);
- warn;
- info;
- debug;
- trace (lowest priority).
To log a message, there are corresponding marcos:
error!()
;warn!()
;info!()
;debug!()
;trace!()
.
These marcos behave like println!
and supports the syntax of format!
.
That is:
{}
callsdisplay()
on an object;{:?}
callsdebug()
on an object;{:#?}
pretty-print the debug formatting.
There are many available implementations
- Simple minimal loggers:
env_logger
- Complex configurable frameworks
log4rs
fern
- Adaptors for other facilities
syslog
By far, the most commonly used logging library in Rust is the env_logger
crate.
env_logger
takes an environment variable RUST_LOG
to configure its log level: std::env::set_var("RUST_LOG", "debug");
.
Initialize logger: env_logger::init();
.
use log::debug;
use log::error;
use log::info;
use log::warn;
fn main() {
env_logger::init();
debug!("Mary has a little lamb");
error!("{}", "Its fleece was white as snow");
info!("{:?}", "And every where that Mary went");
warn!("{:#?}", "The lamb was sure to go");
}
Another logging library in Rust is log4rs
crate.
use log::error;
use log::info;
use log::warn;
use log::{debug, LevelFilter};
use log4rs::append::console::ConsoleAppender;
use log4rs::config::{Appender, Root};
use log4rs::Config;
fn main() {
let stdout = ConsoleAppender::builder().build();
let config = Config::builder().appender(Appender::builder().build("stdout", Box::new(stdout)))
.build(Root::builder().appender("stdout").build(LevelFilter::Trace)).unwrap();
let _handle = log4rs::init_config(config).unwrap();
debug!("Mary has a little lamb");
error!("{}", "Its fleece was white as snow");
info!("{:?}", "And every where that Mary went");
warn!("{:#?}", "The lamb was sure to go");
}