#log-file #logging #stdout #level #filesize #log-level #path

log2

log2 是一个现成的 Rust 日志库

12个版本

0.1.11 2024年2月19日
0.1.10 2023年9月25日
0.1.7 2023年6月12日

13#stdout

Download history 29/week @ 2024-03-11 92/week @ 2024-03-18 16/week @ 2024-03-25 62/week @ 2024-04-01 41/week @ 2024-04-08 10/week @ 2024-04-15 13/week @ 2024-04-22 19/week @ 2024-04-29 5/week @ 2024-05-06 28/week @ 2024-05-13 34/week @ 2024-05-20 21/week @ 2024-05-27 125/week @ 2024-06-03 54/week @ 2024-06-10 27/week @ 2024-06-17 14/week @ 2024-06-24

每月下载量 221次
用于 ws2

MIT 许可

87KB
315

log2

log2 是一个现成的 Rust 日志库。它异步地将日志写入 stdout 或文件,并基于文件大小自动轮转。

用法

添加依赖

cargo add log2

将日志记录到 stdout

简单启动。

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");
}

输出

Screnshot of log2 output

显示模块路径,并设置日志级别。

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");
}

将日志记录到文件

log2 默认文件大小为 100MB,最大文件数为 10,您可以根据需要更改。注意 _log2 将在作用域结束时停止 log2 实例。

use log2::*;

fn main() {
    // configurable way: 
    // - log to file, file size: 100 MB, rotate: 20
    // - tee to stdout
    // - show module path, default is true
    let _log2 = log2::open("log.txt")
                .size(100*1024*1024)
                .rotate(20)
                .tee(true)
		.module(true)
                .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");
}

输出文件

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

依赖项

~1–11MB
~75K SLoC