tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API
项目引入
[dependencies]
tklog = "0.0.3" # "0.0.x" 当前版本
tklog通过调用 uselog()
函数 来启用官方log的API支持
use std::{thread, time::Duration};
use tklog::{Format, LEVEL, LOG};
fn test_synclog() {
//初始化
LOG.set_console(true)
.set_level(LEVEL::Debug)
.set_format(Format::LevelFlag | Format::Microseconds | Format::ShortFileName)
.set_cutmode_by_size("logsize.log", 10000, 10, true)
.uselog(); //启用官方log库
log::trace!("trace>>>>{}{}{}{}{}", "aaaa", 1, 2, 3, 4); //trace级别小于设置的Debug,则不执行打印
log::debug!("debug>>>>{}{}",1,2);
log::info!("info log");
log::warn!("warn log");
log::error!("error log");
thread::sleep(Duration::from_secs(1))
}
[DEBUG] 09:10:56.857540 logapi.rs 19:debug>>>>12
[INFO] 09:10:56.857824 logapi.rs 20:info log
[WARN] 09:10:56.857878 logapi.rs 21:warn log
[ERROR] 09:10:56.857890 logapi.rs 22:error log
use std::{thread, time::Duration};
use tklog::{Format, LEVEL, ASYNC_LOG};
async fn test_synclog() {
//初始化
ASYNC_LOG.set_console(false)
.set_level(LEVEL::Trace)
.set_cutmode_by_size("asynclogsize.log", 10000, 10, true).await
.uselog(); //启用官方log库
log::trace!("trace async log>>>>{}{}{}{}{}", "aaaaaaaaa", 1, 2, 3, 4);
log::debug!("debug async log>>>>{}{}",1,2);
log::info!("info async log");
log::warn!("warn async log");
log::error!("error async log");
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}
[TRACE] 2024-05-31 09:18:34 :trace async log>>>>aaaaaaaaa1234
[DEBUG] 2024-05-31 09:18:34 :debug async log>>>>12
[INFO] 2024-05-31 09:18:34 :info async log
[WARN] 2024-05-31 09:18:34 :warn async log
[ERROR] 2024-05-31 09:18:34 :error async log
调用官方log库API属于全局单例调用,而使用 LOG.uselog() 或 ASYNC_LOG.uselog()启用API 是常规实例与异步实例的不同调用 ,LOG与ASYNC_LOG是不同实例,调用uselog()后,会将log库的实际执行实例设置为本身。在项目中如果反复调用uselog(),要防止后面执行实例覆盖前面实例后可能出现的日志打印问题。