RUST_LOG=error cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.18s Running `target/debug/mylog` [2023-10-03T05:34:28Z ERROR mylog] this is printed by default
do it: RUST_LOG=debug cargo run
output: Finished dev [unoptimized + debuginfo] target(s) in 0.03s Running `target/debug/mylog` [2023-10-03T05:34:43Z DEBUG mylog] this is a debug message [2023-10-03T05:34:43Z ERROR mylog] this is printed by default [2023-10-03T05:34:43Z INFO mylog] the answer was: 12
2. 控制输出等级
Log levels are controlled on a per-module basis, and by default all logging is disabled except for the error level.
日志等级是按照每个模块控制的,默认情况下所有的日志被禁止除了error等级的输出
在模块中控制日志等级,使用env_logger::build创建log的实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
mod mylog;
use log::{debug, error, info, log_enabled, Level, LevelFilter};
➜ log git:(master) ✗ cargo run Compiling mylog v0.1.0 (/Users/yother/WorkSpace/BackEnd/Rust/log) Finished dev [unoptimized + debuginfo] target(s) in 1.19s Running `target/debug/mylog` [2023-10-03T05:48:46Z DEBUG mylog] this is a debug message [2023-10-03T05:48:46Z ERROR mylog] this is printed by default [2023-10-03T05:48:46Z INFO mylog] the answer was: 12