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

相关推荐
花米徐2 小时前
技术洞察精选 | 2026年4月28日 — 5月4日
后端·python·flask
阿维的博客日记2 小时前
Spring Cloud 为什么需要服务注册与发现中心这些东西?
后端·spring·spring cloud
笑而不语2 小时前
13|元数据过滤检索:UserContext 与按用户查知识
后端
Aolith2 小时前
我是怎么把个人论坛首页性能从80分优化到100分的(附踩坑全记录)
vue.js·性能优化
likerhood3 小时前
ConcurrentHashMap详细讲解(java)
java·开发语言·性能优化
用户095367515833 小时前
Go:浮点数如何进行比较?
后端·go
容智信息3 小时前
AI Agent(智能体)的输出格式应该从 Markdown 转向 HTML吗?
前端·人工智能·rust·编辑器·html·prompt
Zeus_3 小时前
如何更好的创建skill
后端
千云3 小时前
AI Coding 落地探索日志 · 初篇 · 启程记
后端·ai编程