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

Rust 日志库 tklog v0.2.0 :支持设置日志级别独立日志文件

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

v0.2.0 版本更新

tklog 支持日志级别所有独立参数设置

  • tklog 通过 set_level_option() 设置日志级别的独立日志参数
  • set_level_option() 接收任意实现 OptionTrait特征的对象

 

示例1 :参数 LevelOption 对象,可以设置日志格式化输出

 #[test] fn testlog() { //将Info级别的日志格式设置为 Format::LevelFlag //将Fatal级别的日志格式设置为 Format::LevelFlag | Format::Date LOG.set_level_option(LEVEL::Info, LevelOption { format: Some(Format::LevelFlag), formatter: None }) .set_level_option(LEVEL::Fatal, LevelOption { format: Some(Format::LevelFlag | Format::Date), formatter: None}); trace!("this is trace log"); debug!("this is debug log"); info!("this is info log"); warn!("this is warn log"); error!("this is error log"); fatal!("this is fatal log"); thread::sleep(Duration::from_secs(1)) }

执行结果

 ---- testlog stdout ---- [DEBUG] 2024-08-24 15:06:02 test_0100.rs 17:this is debug log [INFO] this is info log [WARN] 2024-08-24 15:06:02 test_0100.rs 19:this is warn log [ERROR] 2024-08-24 15:06:02 test_0100.rs 20:this is error log [FATAL] 2024-08-24 this is fatal log

示例2 参数 LogOption 对象,可以设置更多参数,包括设置日志文件

 #[test] fn testlog() { LOG.set_level_option(LEVEL::Info, LogOption { format: Some(Format::LevelFlag), formatter: None, level:None, console: None, fileoption: Some(Box::new(FileTimeMode::new("0200time.log", tklog::MODE::DAY, 0, false))) }) .set_level_option(LEVEL::Fatal, LogOption { format: Some(Format::LevelFlag | Format::Date), formatter: None, level: None, console: None, fileoption: Some(Box::new(FileSizeMode::new("0200size.log", 1<<10, 0, false)))}); trace!("this is trace log"); debug!("this is debug log"); info!("this is info log"); warn!("this is warn log"); error!("this is error log"); fatal!("this is fatal log"); thread::sleep(Duration::from_secs(1)) }

示例说明:

  1. Info级别的文件日志设置为按天分割,文件名 0200time.log
  2. Fatal级别的文件日志设置为按大小分割,文件名 0200size.log

0.2.0版本基准压力测试

说明:

  • log_benchmakk:  tklog 常规打印压力测试
  • mod_benchmark:  mod 模块独立日志文件压力测试
  • level_benchmark :  level 日志级别的独立日志文件压力测试

结论:

  • 模块独立日志文件打印,日志级别的独立日志文件打印 与常规日志文件打印的效率基本一致,没有明显的差异。
  • 模块独立日志文件设置为 0.0.8 版本新增的功能。
原文链接:https://www.oschina.net/news/311306/tklog-0-2-0
关注公众号

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

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

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

文章评论

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

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章