一、添加 log 依赖库
log4rs 是模仿log4j 的java库
1 2 3 | [dependencies] log = "0.4.8" log4rs = "0.11.0" |
二、配置详解
1、
a)
console :控制台file :普通的日志文件rolling_file :可以分割处理的日志文件
b)
c)
d)
json :json格式输出pattern :模式输出,如{d} [{t}] {l} {M}:{m}{n} writer
e)
compound :复合策略,多个策略规则trigger :触发策略kind: size 和limit: 1024 ,按照文件大小,限制1024字节roller :分割策略delete ,超过1024字节,处理方式是删除,也可以使用fixed_window 压缩存储
三、配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | refresh_rate: 30 seconds appenders: stdout: kind: console requests: kind: rolling_file path: "log/requests.log" encoder: kind: json policy: kind: compound trigger: kind: size limit: 10 mb roller: kind: fixed_window pattern: '{0}/requests.log.{{}}' base: 1 count: 5 root: level: info appenders: - stdout - requests |
1 2 3 4 5 6 7 8 9 10 11 12 | #[macro_use] extern crate log; extern crate log4rs; use std::thread; fn main() { log4rs::init_file("log.yaml",Default::default()).unwrap(); info!("Hello, world!"); thread::sleep(std::time::Duration::from_secs(10)); } |