Rust 日志库 tklog 0.3.0 发布

软件资讯 软件资讯 发布于 6小时前 2 浏览 0 回复
tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API,支持mod独立参数设置,支持日志level独立参数设置
  1. 简介
  2. Github地址
  3. 仓库地址
  4. rust日志库性能压测 — log4rs + tracing + tklog
  5. 记录 tklog 压测结果》

v0.3.0 更新内容

该版本主要修复参数Formatter的bug。通过Formatter参数,可以构建json输出格式,可以参数示例 test_0_3_0

示例1

#[test]
fn testlog() {
    LOG.set_cutmode_by_mixed("030test.log", 1 << 15, tklog::MODE::HOUR, 10, false);
    LOG.set_formatter("{\"level\":\"{level}\",\"time\":\"{time}\",\"file\":\"{file}\",\"message\":\"{message}\"}\n").set_console(true);
    trace!("trace!", "this is sync log");
    debug!("debug!", "this is sync log");
    info!("info!", "this is sync log");
    warn!("warn!", "this is sync log");
    error!("error!", "this is sync log");
    fatal!("fata!", "this is sync log");
    thread::sleep(Duration::from_secs(3))
}

输出:

{"level":"[DEBUG]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 11","message":"debug!this is sync log"}
{"level":"[INFO]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 12","message":"info!this is sync log"}
{"level":"[WARN]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 13","message":"warn!this is sync log"}
{"level":"[ERROR]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 14","message":"error!this is sync log"}
{"level":"[FATAL]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 15","message":"fata!this is sync log"}

示例2 修改json的参数

#[test]
fn testlog2() {
    LOG.set_attr_format(|fmt| {
        fmt.set_level_fmt(|level| {
            match level {
                LEVEL::Trace => "trace",
                LEVEL::Debug => "debug",
                LEVEL::Info => "info",
                LEVEL::Warn => "warn",
                LEVEL::Error => "error",
                LEVEL::Fatal => "fatal",
                LEVEL::Off => "",
            }
            .to_string()
        });

        fmt.set_time_fmt(|| {
            let now: DateTime<Local> = Local::now();
            ("".to_string(), "".to_string(), now.format("%Y%m%d %H:%M:%S").to_string())
        });
    });
    LOG.set_formatter("{\"level\":\"{level}\",\"time\":\"{time}\",\"file\":\"{file}\",\"message\":\"{message}\"}\n").set_console(true).set_level(LEVEL::Trace);
    trace!("trace!", "this is sync log");
    debug!("debug!", "this is sync log");
    info!("info!", "this is sync log");
    warn!("warn!", "this is sync log");
    error!("error!", "this is sync log");
    fatal!("fata!", "this is sync log");
    thread::sleep(Duration::from_secs(3))
}

输出

{"level":"trace","time":"20250728 11:10:13","file":"test_0_3_0.rs 41","message":"trace!this is sync log"}
{"level":"debug","time":"20250728 11:10:13","file":"test_0_3_0.rs 42","message":"debug!this is sync log"}
{"level":"info","time":"20250728 11:10:13","file":"test_0_3_0.rs 43","message":"info!this is sync log"}
{"level":"warn","time":"20250728 11:10:13","file":"test_0_3_0.rs 44","message":"warn!this is sync log"}
{"level":"error","time":"20250728 11:10:13","file":"test_0_3_0.rs 45","message":"error!this is sync log"}
{"level":"fatal","time":"20250728 11:10:13","file":"test_0_3_0.rs 46","message":"fata!this is sync log"}


同类文章推荐:

  • IntelliJ IDEA 2025.1.4 现已发布
  • Spring Modulith 2.0 M1 发布
  • FreeFileSync 14.4 发布
  • Spring Boot 4.0.0 M1 发布
  • NumPy 2.3.2 发布
  • Firefox 中国版宣布停止服务