tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API,支持mod独立参数设置,支持日志level独立参数设置
v0.2.4 版本更新
该版本主要更新为:
- 在v0.2.3更新内容的基础上,新增支持日志内容的自定义格式化。通过
set_body_fmt自定义输出日志内容。 - 优化性能,性能提高30%-50%。
通过 set_body_fmt 函数设置日志标识与时间格式
示例: 通过set_body_fmt 设置不同日志级别输出不用颜色的日志
fn testlog() {
LOG.set_level(LEVEL::Trace).set_attr_format(|fmt| {
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[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(),
}
});
});
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))
}
执行结果:
性能压测数据
log_benchmark
测试编号
最小时间 (µs)
最大时间 (µs)
平均时间 (µs)
变化百分比 (%)
p 值
1
2.3949
2.4941
2.4428
-0.5586%
0.14
2
2.3992
2.4632
2.4307
-12.388%
0.00
3
2.4525
2.5632
2.5059
-10.548%
0.00
4
2.5650
2.6775
2.6194
-3.5311%
0.79
mod_benchmark
测试编号
最小时间 (µs)
最大时间 (µs)
平均时间 (µs)
变化百分比 (%)
p 值
1
2.1946
2.2718
2.2325
-2.5723%
0.96
2
2.2126
2.2920
2.2508
-11.895%
0.00
3
2.2603
2.3693
2.3113
-12.539%
0.00
4
2.4908
2.6440
2.5655
-1.3617%
0.29
2. 总结统计
-
log_benchmark
-
最小时间: 2.3949 µs
-
最大时间: 2.6775 µs
-
平均时间: 2.5160 µs
-
变化幅度: 从 -0.5586% 到 -12.388%
-
p 值: 大部分测试显著性强(p < 0.05)。
-
mod_benchmark
-
最小时间: 2.1946 µs
-
最大时间: 2.6440 µs
-
平均时间: 2.3430 µs
-
变化幅度: 从 -2.5723% 到 -12.539%
-
p 值: 大部分测试显著性强(p < 0.05)。
性能统计数据(每次响应时间)
-
最小时间: 2.1946 µs
-
最大时间: 2.6775 µs
-
平均时间: 2.3946 µs
性能优化过程中的火焰图变化
可以看出到,tklog的性能在优化过程中有效提高。