Skip to content

Releases: donnie4w/tklog

v0.2.9

30 Dec 02:04
Compare
Choose a tag to compare

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

12 Dec 09:50
Compare
Choose a tag to compare

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

19 Nov 10:23
Compare
Choose a tag to compare

version 0.2.7

fix the bug #14

v0.2.6

04 Nov 05:16
Compare
Choose a tag to compare

version 0.2.6

修复时间分割的bug

说明:旧版本在时间分割时,没有判断大单位时间,存在bug,该版本已经修复

v0.2.5

31 Oct 02:35
Compare
Choose a tag to compare

version 0.2.5

update:

  1. ADD RUST_LOG
    • RUST_LOG is an environment variable used to control the logging levels in Rust applications.
  2. Optimize performance

v0.2.4

21 Oct 12:53
Compare
Choose a tag to compare

version 0.2.4
更新内容

v0.2.3

18 Oct 02:14
Compare
Choose a tag to compare

version 0.2.3
版本更新

v0.2.2

09 Oct 03:16
Compare
Choose a tag to compare

version0.2.2
更新内容

v0.2.1

30 Sep 06:45
Compare
Choose a tag to compare

version0.2.1
更新内容

v0.2.0

07 Sep 03:53
Compare
Choose a tag to compare

增加支持日志级别设置独立日志格式参数,设置日志文件

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