scrub_log

Crates.ioscrub_log
lib.rsscrub_log
version0.2.1
sourcesrc
created_at2019-12-03 07:37:38.672623
updated_at2020-01-26 08:49:28.614248
descriptionLog formatter with colors, file locations, program run duration.
homepage
repositoryhttps://github.com/glalonde/scrub_log
max_upload_size
id186073
size23,460
(glalonde)

documentation

README

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();
Commit count: 16

cargo fmt