Releases: donnie4w/tklog
Releases · donnie4w/tklog
v0.2.9
v0.2.9 更新内容
该版本主要实现支持 按时间与文件大小混合模式切割日志文件
两者方式可以设置
- 通过 set_cutmode_by_mixed 设置
- 通过 set_option 设置
- 测试程序地址: test_0_2_9.rs
一. 调用 .set_cutmode_by_mixed() 函数,参数:
- 文件路径
- 指定文件滚动大小
- 时间模式
- 最大备份日志文件数
- 是否压缩备份的日志文件
#[test]
fn testlog() {
LOG.set_cutmode_by_mixed("tklogs.log", 1 << 15,tklog::MODE::HOUR, 10, false);
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(3))
}
二. 调用 .set_option() 函数
#[test]
fn testlog2() {
let mut lo = tklog::LogOption::new();
lo.set_fileoption(tklog::handle::FileMixedMode::new("tklogs.log", 1 << 15,tklog::MODE::DAY, 10, false));
LOG.set_option(lo);
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(3))
}
日志文件切分的结果:
按天与大小混合备份日期文件,如:
- tklogs_20240521_1.log
- tklogs_20240521_2.log
- tklogs_20240521_3.log
- tklogs_20240521_4.log
- tklogs_20240522_1.log
- tklogs_20240522_2.log
- tklogs_20240522_3.log
- tklogs_20240522_4.log
按小时与大小混合备份日志文件,如:
- tklogs_2024052110_1.log
- tklogs_2024052110_2.log
- tklogs_2024052110_3.log
- tklogs_2024052211_1.log
- tklogs_2024052211_2.log
- tklogs_2024052211_3.log
按月份与大小混合备份日志文件,如:
- tklogs_202403_1.log
- tklogs_202403_2.log
- tklogs_202403_3.log
- tklogs_202404_1.log
- tklogs_202404_2.log
- tklogs_202404_3.log
v0.2.8
version 0.2.8
- 增加 控制台日志独立格式化功能
- 通过
set_console_body_fmt
可以设置日志内容在控制台的显示格式,与set_body_fmt
类似,不同的是set_body_fmt
对控制台信息与文件信息均有效。 - 测试程序地址: test_0_2_8.rs
示例:
fn testlog2() {
LOG.set_console(true).set_cutmode_by_size("028test2.log", 1 << 20, 0, false).set_level(LEVEL::Trace).set_attr_format(|fmt| {
fmt.set_console_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(),
}
});
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[44m", trimmed_body), //背景蓝色
LEVEL::Debug => format!("{}{}", "\x1b[46m", trimmed_body), //背景青色
LEVEL::Info => format!("{}{}", "\x1b[42m", trimmed_body), //背景绿色
LEVEL::Warn => format!("{}{}", "\x1b[43m", trimmed_body), //背景黄色
LEVEL::Error => format!("{}{}", "\x1b[41m", trimmed_body), //背景红色
LEVEL::Fatal => format!("{}{}", "\x1b[45m", 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))
}
说明:示例对控制台日志进行独立设置
输出结果
控制台日志输出:
文件日志输出:
0.2.7
v0.2.6
v0.2.5
v0.2.4
v0.2.3
v0.2.2
v0.2.1
v0.2.0
增加支持日志级别设置独立日志格式参数,设置日志文件
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))
}
示例说明:
- Info级别的文件日志设置为按天分隔,文件名
0200time.log
- Fatal级别的文件日志设置为按大小分隔,文件名
0200size.log