15 releases

new 0.1.14 Nov 20, 2024
0.1.13 Oct 25, 2024
0.1.11 Feb 19, 2024
0.1.10 Sep 25, 2023
0.1.7 Jun 12, 2023

#607 in Debugging

Download history 170/week @ 2024-07-31 85/week @ 2024-08-07 68/week @ 2024-08-14 91/week @ 2024-08-21 69/week @ 2024-08-28 11/week @ 2024-09-04 41/week @ 2024-09-11 66/week @ 2024-09-18 99/week @ 2024-09-25 117/week @ 2024-10-02 231/week @ 2024-10-09 156/week @ 2024-10-16 472/week @ 2024-10-23 107/week @ 2024-10-30 85/week @ 2024-11-06 127/week @ 2024-11-13

813 downloads per month
Used in ws2

MIT license

87KB
334 lines

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() {
    let _log2 = 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() {
    let _log2 = 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. Note the _log2 will stop the log2 instance when it is out of the scope.

use log2::*;

fn main() {
    // configurable way: 
    // - log to file, file size: 100 MB, rotate: 20
    // - tee to stdout
    // - show module path, default is true
    // - filter with matched module
    let _log2 = log2::open("log.txt")
                .size(100*1024*1024)
                .rotate(20)
                .tee(true)
                .module(true)
                .module_filter(|module| module.contains(""))
                .start();

    // out-of-the-box way
    // let _log2 = 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

Dependencies

~1–11MB
~61K SLoC