Crates.io | scrub_log |
lib.rs | scrub_log |
version | 0.2.1 |
source | src |
created_at | 2019-12-03 07:37:38.672623 |
updated_at | 2020-01-26 08:49:28.614248 |
description | Log formatter with colors, file locations, program run duration. |
homepage | |
repository | https://github.com/glalonde/scrub_log |
max_upload_size | |
id | 186073 |
size | 23,460 |
A log formatter that has a default with colors, filename, line number, and program runtime.
Add this crate to Cargo.toml
[dependencies]
scrub_log = "0.1.1"
Now you can easily print a nice log line for all your printf debugging needs, glog style.
use log::{debug, error, info, trace, warn};
fn main() {
scrub_log::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 [log_example:10] Lorem ipsum
DEBUG]42.350µs [log_example:11] dolor sit amet,
INFO ]45.667µs [log_example:12] consectetur adipiscing elit,
WARN ]48.431µs [log_example:13] ed do eiusmod
ERROR]50.754µs [log_example: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! {
--log_filter: &str = "log_example=info"
}
fn main() {
gflags::parse();
scrub_log::init_with_filter_string(LOG_FILTER.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 log_example -- --log_filter warn
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/examples/log_example --log_filter warn`
WARN ]11.740µs [log_example:13] ed do eiusmod
ERROR]44.914µs [log_example:14] tempor incididunt
$ cargo run --example log_example -- --log_filter log_example=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 [log_example:11] dolor sit amet,
INFO ]42.239µs [log_example:12] consectetur adipiscing elit,
WARN ]45.890µs [log_example:13] ed do eiusmod
ERROR]48.758µs [log_example: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();