動態

詳情 返回 返回

tklog :Rust靈活高效日誌管理 - 動態 詳情

tklog v0.0.9 :Rust靈活高效日誌管理

tklog是rust高性能結構化日誌庫,支持同步日誌,異步日誌,支持自定義日誌的輸出格式,支持按時間,按文件大小分割日誌文件,支持日誌文件壓縮備份,支持官方日誌庫標準API,支持mod獨立參數設置
  1. 官網
  2. 項目源碼
  3. 倉庫
  4. 《tklog與log4rs 的基準測試》

核心特點

  • 高性能同步與異步日誌記錄:tklog 支持高效的同步與異步日誌記錄,確保即使在高負載環境下也能保持良好的性能。
  • 靈活的日誌格式定製:用户可以根據需要自定義日誌輸出格式,包括日誌級別、時間戳格式等。
  • 智能日誌文件管理:支持按時間或文件大小自動分割日誌文件,以及文件數量的滾動管理,有助於維持日誌目錄的整潔。
  • 日誌壓縮與備份:支持對日誌文件進行壓縮歸檔,方便長期存儲和備份。
  • 官方標準 API 兼容:與 Rust 官方日誌庫標準 API 兼容,便於集成使用。
  • 模塊級配置:允許在不同的模塊中獨立設置日誌參數,增強了靈活性。

0.0.9 版本更新

  • v0.0.9 版本,tklog 引入了自定義日誌處理函數的功能,開發者可以通過 set_custom_handler() 方法來定義自己的日誌處理邏輯。
説明:custom_handler 來自 bronya0 給 go-logger 添加的等價功能 CustomHandler, 該功能在go編程中非常實用,在rust中同樣很實用,它可以由開發者通過捕獲日誌記錄時的日誌級別,日誌模塊,文件名等信息,做必要的業務處理,如捕獲error日誌進行郵件通知等。因此在同為日誌框架的tklog添加相同的功能,可以分別在同步日誌與異步日誌中添加
  • LOG.set_custom_handler(custom_handler) 同步
  • ASYNC_LOG.set_custom_handler(custom_handler) 異步

自定義日誌處理函數示例

下面是一個簡單的示例,展示瞭如何使用自定義日誌處理器 custom_handler 來控制日誌的輸出。

#[test]
fn test_custom() {
    fn custom_handler(lc: &LogContext) -> bool {
        println!("Level: {:?}", lc.level);
        println!("Message: {:?}", lc.log_body);
        println!("Filename: {:?}", lc.filename);
        println!("Line: {:?}", lc.line);
        println!("Module Name: {:?}", lc.modname);

        // 如果日誌級別為 Debug,則不進行進一步處理
        if lc.level == LEVEL::Debug {
            println!("Debug Now");
            return false;
        }
        true
    }

    LOG.set_custom_handler(custom_handler);
    debug!("000000000000000000");
    info!("1111111111111111111");
    thread::sleep(Duration::from_secs(1));
}
執行結果
---- test_custom stdout ----
level >>>>>>>>>>>>>>>>>Debug
message >>>>>>>>>>>>>>>>>"000000000000000000"
filename >>>>>>>>>>>>>>>>>"tests    estsynclog.rs"
line >>>>>>>>>>>>>>>>>143
modname >>>>>>>>>>>>>>>>>"testsynclog"
debug now
level >>>>>>>>>>>>>>>>>Info
message >>>>>>>>>>>>>>>>>"1111111111111111111"
filename >>>>>>>>>>>>>>>>>"tests    estsynclog.rs"
line >>>>>>>>>>>>>>>>>144
modname >>>>>>>>>>>>>>>>>"testsynclog"
[INFO] 2024-08-05 15:39:07 testsynclog.rs 144:1111111111111111111

使用方式

  • 添加依賴

    [dependencies]
    tklog = "0.0.9"  #   "0.0.x" 當前版本
  • 基本日誌記錄

    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);
    }

    打印結果

    [TRACE] 2024-05-26 11:47:22 testlog.rs 27:trace>>>>,aaaaaaaaa,1,2,3,4
    [DEBUG] 2024-05-26 11:47:22 testlog.rs 28:debug>>>>,bbbbbbbbb,1,2,3,5
    [INFO] 2024-05-26 11:47:22 testlog.rs 29:info>>>>,ccccccccc,1,2,3,5
    [WARN] 2024-05-26 11:47:22 testlog.rs 30:warn>>>>,dddddddddd,1,2,3,6
    [ERROR] 2024-05-26 11:47:22 testlog.rs 31:error>>>>,eeeeeeee,1,2,3,7
    [FATAL] 2024-05-26 11:47:22 testlog.rs 32:fatal>>>>,ffffffff,1,2,3,8
user avatar u_15988698 頭像 vanve 頭像 shumile_5f6954c414184 頭像 xiaodiandideyangrouchuan 頭像 buildyuan 頭像 aipaobudeshoutao 頭像 binghe001 頭像 aitibao_shichangyingxiao 頭像 kubesphere 頭像 manshenjiroudehuajuan 頭像 chenbimo 頭像 phytium_developers 頭像
點贊 29 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.