tklog0.2.8—Rust高性能日志库

tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API,支持mod独立参数设置,支持日志level独立参数设置

  1. 简介
  2. Github地址
  3. 仓库地址
  4. rust日志库性能压测 --- log4rs + tracing + tklog

v0.2.8 更新内容

  • 增加 控制台日志独立格式化功能
  • 通过 set_console_body_fmt 可以设置日志内容在控制台的显示格式,与 set_body_fmt 类似,不同的是 set_body_fmt 对控制台信息与文件信息均有效。
  • 测试程序地址: test_0_2_8.rs

示例:

rust 复制代码
fn testlog2() {
    LOG.set_console(true).set_cutmode_by_size("028test2.log", 1 << 20, 0, false).set_level(LEVEL::Trace).set_attr_format(|fmt| {
        fmt.set_console_body_fmt(|level, body| {
            //处理body的末尾换行符
            let trimmed_body = if body.ends_with('\n') { format!("{}{}", body.as_str()[..body.len() - 1].to_string(), "\x1b[0m\n") } else { format!("{}{}", body, "\x1b[0m\n") };

            match level {
                LEVEL::Trace => format!("{}{}", "\x1b[34m", trimmed_body), //蓝色
                LEVEL::Debug => format!("{}{}", "\x1b[36m", trimmed_body), //青色
                LEVEL::Info => format!("{}{}", "\x1b[32m", trimmed_body),  //绿色
                LEVEL::Warn => format!("{}{}", "\x1b[33m", trimmed_body),  //黄色
                LEVEL::Error => format!("{}{}", "\x1b[31m", trimmed_body), //红色
                LEVEL::Fatal => format!("{}{}", "\x1b[41m", trimmed_body), //背景红
                LEVEL::Off => "".to_string(),
            }
        });

        fmt.set_body_fmt(|level, body| {
            //处理body的末尾换行符
            let trimmed_body = if body.ends_with('\n') { format!("{}{}", body.as_str()[..body.len() - 1].to_string(), "\x1b[0m\n") } else { format!("{}{}", body, "\x1b[0m\n") };
            match level {
                LEVEL::Trace => format!("{}{}", "\x1b[44m", trimmed_body), //背景蓝色
                LEVEL::Debug => format!("{}{}", "\x1b[46m", trimmed_body), //背景青色
                LEVEL::Info => format!("{}{}", "\x1b[42m", trimmed_body),  //背景绿色
                LEVEL::Warn => format!("{}{}", "\x1b[43m", trimmed_body),  //背景黄色
                LEVEL::Error => format!("{}{}", "\x1b[41m", trimmed_body), //背景红色
                LEVEL::Fatal => format!("{}{}", "\x1b[45m", trimmed_body), //背景紫色
                LEVEL::Off => "".to_string(),
            }
        });
    });

    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(1))
}

说明:示例对控制台日志进行独立设置

输出结果

控制台日志输出:
文件日志输出:

可以看到,当 调用 set_console_body_fmt 设置控制台日志时,控制台与文件将分别输出设置的日志格式。


tklog 快速使用

安装tklog

方法一:使用 cargo add****命令

复制代码
cargo add tklog

方法二:手动编辑 Cargo.toml

复制代码
tklog = "0.2.8"

测试用例

rust 复制代码
use tklog::{trace,debug, error, fatal, info,warn}
fn testlog() {
    trace!("trace>>>>", "aaaaaaaaa", 1, 2, 3, 4);
    debug!("debug>>>>", "bbbbbbbbb", 1, 2, 3, 5);
    info!("info>>>>", "ccccccccc", 1, 2, 3, 5);
    warn!("warn>>>>", "dddddddddd", 1, 2, 3, 6);
    error!("error>>>>", "eeeeeeee", 1, 2, 3, 7);
    fatal!("fatal>>>>", "ffffffff", 1, 2, 3, 8);
}

tklog性能

Tklog 有极高的性能,特别是在linux环境中,具体测试参考文章

在linux环境中,tklog比同类型的日志库性能高10倍以上,在windows环境中,性能则为同类型日志库2倍左右

详细使用方法请参考 readme.md

相关推荐
砍材农夫10 小时前
物联网实战:Spring Boot MQTT | MQTT 设备模拟器演示(附源码)
java·spring boot·后端·物联网·spring·netty
我叫黑大帅11 小时前
解决聊天页内部滚轮改为页面滚动问题
javascript·后端·面试
IT_陈寒11 小时前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
ct97812 小时前
Three.js 性能优化(测量-定位-优化)
javascript·性能优化·three
zhangxingchao12 小时前
AI应用开发八:RAG相关技术总结
前端·人工智能·后端
吴佳浩12 小时前
Go史上最大“打脸”现场来了:泛型方法终于实现了
后端·go
Huyuejia12 小时前
runtime-ask
后端
Rust研习社12 小时前
90% 的 Rust 新手都不知道的 3 个实用开发技巧
后端·rust·编程语言
ZengLiangYi13 小时前
sql.js WASM 深度解析
javascript·数据库·后端
Stick_ZYZ13 小时前
从“能调用工具”到“能稳定执行任务”:Agent 工程化的下一步
java·人工智能·后端·spring·ai