< 返回版块

teshin 发表于 2021-05-16 14:17

Tags:rust,log,日志

前言

rust终将君临天下。但现在还处于发展的早期,虽然有很多日志包,但没有一款日志包用的顺手。这里推荐wd_log板条箱。非常使用,倾情推荐。 支持功能如下:

  • 打印等级设置
  • 打印选项设置
  • 自定义日志头
  • 终端多种颜色打印
  • 支持输出到文件
  • 格式化输出
  • result自动处理
  • 多字段组合输出 (coding)

简介

支持的日志等级

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • PANIC

简单栗子

[dependencies]
wd_log = "0.1"
fn main() {
    //设置打印级别为INFO,更多设置参考文档
    wd_log::set_level(wd_log::INFO);
    //以_ln结尾的宏会自带换行
    wd_log::log_debug_ln!("hello world");
    wd_log::log_info_ln!("{} {}","hello","world");
    wd_log::log_warn_ln!("hello world");
    wd_log::log_error_ln!("{:?}","hello world");
}

打印效果如下: 在这里插入图片描述

  • 注意: log_panic 会在打印后,直接panic进程,测试如下:
#[test]
#[should_panic]
fn test_panic(){
    wd_log::log_panic!("hello world")
}

更多用法

  • 以res_开头的宏,可以对result类型进行自动处理,如下:
    let result = std::fs::File::open("test.txt");
    //如果result为Ok(T) 则返回Some(T)
    //如果result为Err(_) 则打印并返回None
    let file = wd_log::res_error_ln!(result);
  • res_panic 用法如下:
    let result = std::fs::File::open("test.txt");
    //如果result为Ok(T) 则返回T
    //如果result为Err(_) 则打印错误并panic
    let file = wd_log::res_panic!(result);

文档

评论区

写评论

还没有评论

1 共 0 条评论, 1 页