A log formatter that has a default with colors, filename, line number, and program runtime.
Add this crate to Cargo.toml
toml
[dependencies]
scrub_log = "0.1.1"
Now you can easily print a nice log line for all your printf debugging needs, glog style.
```rust use log::{debug, error, info, trace, warn};
fn main() {
scrublog::init().unwrap();
trace!("Lorem ipsum");
debug!("dolor sit amet,");
info!("consectetur adipiscing elit,");
warn!("ed do eiusmod");
error!("tempor incididunt");
}
Example output:
TRACE]15.464µs [logexample:10] Lorem ipsum
DEBUG]42.350µs [logexample:11] dolor sit amet,
INFO ]45.667µs [logexample:12] consectetur adipiscing elit,
WARN ]48.431µs [logexample:13] ed do eiusmod
ERROR]50.754µs [logexample:14] tempor incididunt
```
This library imports the filter functionality of env_logger
, so it is easy to add a filter string in that same format.
One convenient way to do so is with a command line flag rather than an environment variable. For example, using the gflags
library:
```
use log::{debug, error, info, trace, warn};
gflags::define! { --logfilter: &str = "logexample=info" }
fn main() {
gflags::parse();
scrublog::initwithfilterstring(LOGFILTER.flag).unwrap();
trace!("Lorem ipsum");
debug!("dolor sit amet,");
info!("consectetur adipiscing elit,");
warn!("ed do eiusmod");
error!("tempor incididunt");
}
This allows one to request different log levels on demand, with a default:
$ cargo run --example logexample -- --logfilter warn
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running target/debug/examples/log_example --log_filter warn
WARN ]11.740µs [logexample:13] ed do eiusmod
ERROR]44.914µs [logexample:14] tempor incididunt
$ cargo run --example logexample -- --logfilter logexample=debug
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running target/debug/examples/log_example --log_filter log_example=debug
DEBUG]14.542µs [logexample:11] dolor sit amet,
INFO ]42.239µs [logexample:12] consectetur adipiscing elit,
WARN ]45.890µs [logexample:13] ed do eiusmod
ERROR]48.758µs [logexample:14] tempor incididunt
```
See the env_logger
docs for exact specifications for the filter string, but generally it is either $global_log_level
or $module1_name=$module1_log_level,$module2_name=$module2_log_level
.
The default log level, inherited from env_logger
, is warn
. This can be changed by specifying a filter string e.g:
scrub_log::init_with_filter_string("trace").unwrap();