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

tklog 0.2.3 — Rust 高性能日志库,日志标识格式自定义设置

日期:2024-10-18点击:126
tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API,支持mod独立参数设置,支持日志level独立参数设置
  1. 简介
  2. Github地址
  3. 仓库地址

v0.2.3 版本更新

新增支持对日志属性标识进行格式化设置,如: [DEBUG] [INFO][WARN][ERROR][FATAL]标识,时间格式化标识

通过 set_attr_format 函数设置日志标识与时间格式

示例1:同步日志打印

 fn testlog() { tklog::LOG.set_attr_format(|fmt| { fmt.set_level_fmt(|level| { match level { LEVEL::Trace => "[T]", LEVEL::Debug => "[D]", LEVEL::Info => "[I]", LEVEL::Warn => "[W]", LEVEL::Error => "[E]", LEVEL::Fatal => "[F]", LEVEL::Off => "", }.to_string() }); fmt.set_time_fmt(|| { let now: DateTime<Local> = Local::now(); (now.format("%Y/%m/%d").to_string(), now.format("%H:%M:%S").to_string(), "".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)) }

执行结果:

 [D] 2024/10/17 19:41:20 test_0230.rs 32:debug!this is sync log [I] 2024/10/17 19:41:20 test_0230.rs 33:info!this is sync log [W] 2024/10/17 19:41:20 test_0230.rs 34:warn!this is sync log [E] 2024/10/17 19:41:20 test_0230.rs 35:error!this is sync log [F] 2024/10/17 19:41:20 test_0230.rs 36:fata!this is sync log

示例2:异步日志打印

 #[tokio::test] async fn asynctestlog() { ASYNC_LOG.set_attr_format(|fmt| { fmt.set_level_fmt(|level| { match level { LEVEL::Trace => "[AT]", LEVEL::Debug => "[AD]", LEVEL::Info => "[AI]", LEVEL::Warn => "[AW]", LEVEL::Error => "[AE]", LEVEL::Fatal => "[AF]", LEVEL::Off => "", } .to_string() }); }); async_trace!("trace!", "this is async log"); async_debug!("debug!", "this is async log"); async_info!("info!", "this is async log"); async_warn!("warn!", "this is async log"); async_error!("error!", "this is async log"); async_fatal!("fata!", "this is async log"); tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; }
原文链接:https://www.oschina.net/news/316647/tklog-0-2-3-released
关注公众号

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

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

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

文章评论

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

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章