Skip to main content

Crate log2

Crate log2 

Source
Expand description

§log2

log2 is an out-of-the-box logging library for Rust. It writes to stdout or to file asynchronousely, and automatically rotates based on file size.

§Usage

§Add dependency

cargo add log2

§Log to stdout

Simple to start.

use log2::*;

fn main() {
log2::start();

trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
}

Output

Screnshot of log2 output

Hide module path, and set log level.

use log2::*;

fn main() {
log2::stdout()
.module(false)
.level("info")
.start();

trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
}

§Log to file

log2 with default file size 100MB, max file count 10, you can change as you like.

use log2::*;

fn main() {
// configurable way:
// - log to file, file size: 100 MB, rotate: 20
// - tee to stdout
// - show module path, default is true
// - show module line, default is false
// - filter with matched module
// - enable gzip compression for aged file
// - custom fomatter support
log2::open("log.txt")
.size(100*1024*1024)
.rotate(20)
.tee(true)
.module(true)
.module_with_line(true)
.module_filter(|module| module.contains(""))
.compress(false)
.format(|record, tee| format!("[{}] [{}] {}\n", chrono::Local::now(), record.level(), record.args()))
.start();

// out-of-the-box way
// log2::open("log.txt").start();

trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
}

Output files

log.txt
log.1.txt
log.2.txt
log.3.txt
log.4.txt
log.5.txt
log.6.txt
log.7.txt
log.8.txt
log.9.txt

Output compressed files

log.txt
log.1.txt.gz
log.2.txt.gz
log.3.txt.gz
log.4.txt.gz
log.5.txt.gz
log.6.txt.gz
log.7.txt.gz
log.8.txt.gz
log.9.txt.gz

§Filter logs by module

Use app!() macro to get the package name from Cargo.toml, and solo() to filter logs to only show those from the current application.

use log2::*;

fn main() {
log2::stdout()
    .solo(app!())
    .start();

info!("this will be shown");
}

This is shorthand for:

log2::stdout()
    .module_filter(|m| m.starts_with(app!()))
    .start();

Macros§

app
Get the application name from Cargo.toml package name. Expands to env!("CARGO_PKG_NAME").
debug
log macros Logs a message at the debug level.
error
log macros Logs a message at the error level.
info
log macros Logs a message at the info level.
trace
log macros Logs a message at the trace level.
warn
log macros Logs a message at the warn level.

Structs§

Handle
handle for manipulating log2
Log2

Functions§

handle
get the global log2 handle for manipulation
open
log to file
reset
reset the global log2 handle (useful for testing)
set_level
set the log level, the input can be both enum or name
start
start the log2 instance by default the logger lives for the entire program duration, no need to store the handle
stdout
create a log2 instance to stdout

Type Aliases§

level
log levels