tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API,支持mod独立参数设置,支持日志level独立参数设置
- 简介
- Github地址
- 仓库地址
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;
}