#log #terminal #file #benchmark

cli-log

a simple logging and timing facility configured with an env variable

3 releases (stable)

1.1.0 Feb 23, 2021
1.0.0 Feb 19, 2021
0.1.0 Dec 31, 2020

#23 in Profiling

Download history 49/week @ 2020-12-29 354/week @ 2021-01-05 304/week @ 2021-01-12 252/week @ 2021-01-19 269/week @ 2021-01-26 316/week @ 2021-02-02 368/week @ 2021-02-09 241/week @ 2021-02-16 614/week @ 2021-02-23

720 downloads per month
Used in 5 crates

MIT license

13KB
177 lines

MIT Latest Version docs Chat on Miaou

cli-log

The boilerplate to have some file logging with a level given by an environment variable, and a facility to log execution durations according to the relevant log level.

It's convenient for terminal applications because you don't want to mix log with stdout or stderr.

And the use of an env variable makes it possible to distribute the application and have users generate some logs without recompilation or configuration.

The names of the log file and the env variable are computed from the name of the application.

So log initialization is just

init_cli_log!();

Import

In Cargo.toml:

log = "0.4"
cli-log = "1.1"

Usage

Here's a complete application using cli-log (it can be found in examples):

#[macro_use] extern crate log;
#[macro_use] extern crate cli_log;

#[derive(Debug)]
struct AppData {
    count: usize,
}
impl AppData {
    fn compute(&mut self) {
        self.count += 7;
    }
}

fn main() {
    init_cli_log!();
    let mut app_data = AppData { count: 35 };
    time!(Debug, app_data.compute());
    info!("count is {}", app_data.count);
    debug!("data: {:#?}", &app_data);
    warn!("this application does nothing");
    info!("bye");
}

If you don't set any SMALL_APP_LOG env variable, there won't be any log.

A convenient way to set the env variable is to launch the app as

SMALL_APP_LOG=debug small-app

or, during development,

SMALL_APP_LOG=debug cargo run

This creates a small-app.log file containing information like the level, app version, and of course the log operations you did with time precise to the ms and the logging module (target):

11:45:37.565 [INFO] cli_log: Starting small-app v0.1.0 with log level DEBUG
11:45:37.565 [DEBUG] small_app: app_data.compute() took 198ns
11:45:37.565 [INFO] small_app: count is 42
11:45:37.565 [DEBUG] small_app: data: AppData {
    count: 42,
}
11:45:37.565 [WARN] small_app: this application does nothing
11:45:37.565 [INFO] small_app: bye

This log file can typically be followed with tail -f small_app.log.

Dependencies

~1MB
~14K SLoC