您现在的位置是:首页 > 文章详情

tklog 0.2.8 — Rust 高性能日志库

日期:2024-12-13点击:70
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

示例:

 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"

 

测试用例

 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

原文链接:https://www.oschina.net/news/324731/tklog-0-2-8
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章