Skip to content

Releases: donnie4w/tklog

v0.3.0

28 Jul 03:11

Choose a tag to compare

v0.3.0 更新内容

该版本主要修复参数Formatter的bug
通过Formatter参数,可以构建json输出格式,可以参数示例 test_0_3_0

示例

#[test]
fn testlog() {
    LOG.set_cutmode_by_mixed("030test.log", 1 << 15, tklog::MODE::HOUR, 10, false);
    LOG.set_formatter("{\"level\":\"{level}\",\"time\":\"{time}\",\"file\":\"{file}\",\"message\":\"{message}\"}\n").set_console(true);
    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))
}

输出:

{"level":"[DEBUG]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 11","message":"debug!this is sync log"}
{"level":"[INFO]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 12","message":"info!this is sync log"}
{"level":"[WARN]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 13","message":"warn!this is sync log"}
{"level":"[ERROR]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 14","message":"error!this is sync log"}
{"level":"[FATAL]","time":"2025-07-28 11:09:09","file":"test_0_3_0.rs 15","message":"fata!this is sync log"}

示例2 修改json的参数

#[test]
fn testlog2() {
    LOG.set_attr_format(|fmt| {
        fmt.set_level_fmt(|level| {
            match level {
                LEVEL::Trace => "trace",
                LEVEL::Debug => "debug",
                LEVEL::Info => "info",
                LEVEL::Warn => "warn",
                LEVEL::Error => "error",
                LEVEL::Fatal => "fatal",
                LEVEL::Off => "",
            }
            .to_string()
        });

        fmt.set_time_fmt(|| {
            let now: DateTime<Local> = Local::now();
            ("".to_string(), "".to_string(), now.format("%Y%m%d %H:%M:%S").to_string())
        });
    });
    LOG.set_formatter("{\"level\":\"{level}\",\"time\":\"{time}\",\"file\":\"{file}\",\"message\":\"{message}\"}\n").set_console(true).set_level(LEVEL::Trace);
    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))
}

输出

{"level":"trace","time":"20250728 11:10:13","file":"test_0_3_0.rs 41","message":"trace!this is sync log"}
{"level":"debug","time":"20250728 11:10:13","file":"test_0_3_0.rs 42","message":"debug!this is sync log"}
{"level":"info","time":"20250728 11:10:13","file":"test_0_3_0.rs 43","message":"info!this is sync log"}
{"level":"warn","time":"20250728 11:10:13","file":"test_0_3_0.rs 44","message":"warn!this is sync log"}
{"level":"error","time":"20250728 11:10:13","file":"test_0_3_0.rs 45","message":"error!this is sync log"}
{"level":"fatal","time":"20250728 11:10:13","file":"test_0_3_0.rs 46","message":"fata!this is sync log"}

v0.2.9

30 Dec 02:04

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

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

Choose a tag to compare

version 0.2.7

fix the bug #14

v0.2.6

04 Nov 05:16

Choose a tag to compare

version 0.2.6

修复时间分割的bug

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

v0.2.5

31 Oct 02:35

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

Choose a tag to compare

version 0.2.4
更新内容

v0.2.3

18 Oct 02:14

Choose a tag to compare

version 0.2.3
版本更新

v0.2.2

09 Oct 03:16

Choose a tag to compare

version0.2.2
更新内容

v0.2.1

30 Sep 06:45

Choose a tag to compare

version0.2.1
更新内容