動態

詳情 返回 返回

tklog0.2.8—Rust高性能日誌庫 - 動態 詳情

tklog是rust高性能結構化日誌庫,支持同步日誌,異步日誌,支持自定義日誌的輸出格式,支持按時間,按文件大小分割日誌文件,支持日誌文件壓縮備份,支持官方日誌庫標準API,支持mod獨立參數設置,支持日誌level獨立參數設置
  1. 簡介
  2. Github地址
  3. 倉庫地址
  4. 《rust日誌庫性能壓測 — log4rs + tracing + tklog》

v0.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))
}
説明:示例對控制枱日誌進行獨立設置

輸出結果

控制枱日誌輸出:

文件日誌輸出:


tklog 快速使用

安裝tklog

方法一:使用 cargo add 命令

cargo add tklog

方法二:手動編輯 Cargo.toml

tklog = "0.2.8"

測試用例

use tklog::{trace,debug, error, fatal, info,warn}
fn testlog() {
    trace!("trace>>>>", "aaaaaaaaa", 1, 2, 3, 4);
    debug!("debug>>>>", "bbbbbbbbb", 1, 2, 3, 5);
    info!("info>>>>", "ccccccccc", 1, 2, 3, 5);
    warn!("warn>>>>", "dddddddddd", 1, 2, 3, 6);
    error!("error>>>>", "eeeeeeee", 1, 2, 3, 7);
    fatal!("fatal>>>>", "ffffffff", 1, 2, 3, 8);
}

tklog性能

Tklog 有極高的性能,特別是在linux環境中,具體測試參考文章

  • 《記錄tklog壓測結果》
  • 《Rust日誌庫性能壓測 — log4rs + tracing + tklog》
在linux環境中,tklog比同類型的日誌庫性能高10倍以上,在windows環境中,性能則為同類型日誌庫2倍左右
詳細使用方法請參考 readme.md
user avatar leixue 頭像 teamcode 頭像 daqianduan 頭像 yian 頭像 wnhyang 頭像 cbuc 頭像 renzhendezicai 頭像 fuzhengwei 頭像 kinfuy 頭像 laoshideyangrouchuan 頭像 ximinghui 頭像 abelethan 頭像
點贊 34 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.