#log #logging #colors #duration #formatter #runtime #print #file

scrub_log

Log formatter with colors, file locations, program run duration

4 releases

0.2.1 Jan 26, 2020
0.2.0 Jan 3, 2020
0.1.1 Dec 4, 2019
0.1.0 Dec 3, 2019

#270 in Debugging

MIT license

8KB
111 lines

scrub_log

Build Status Crates.io MIT

A log formatter that has a default with colors, filename, line number, and program runtime.

Usage

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

Image of text

Filters

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();

Dependencies

~1.3–1.8MB
~46K SLoC