一个实用的rust日志板条箱,灰常好用
@TOC
前言
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);
文档
本作品采用《CC 协议》,转载必须注明作者和本文链接