Skip to content

Latest commit

 

History

History
56 lines (45 loc) · 1.4 KB

File metadata and controls

56 lines (45 loc) · 1.4 KB

启用每个模块的日志级别

[![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]

创建两个模块:foo,以及嵌套的foo::bar,它们的记录指令,单独用RUST_LOG环境变量控制。

#[macro_use]
extern crate log;
extern crate env_logger;

mod foo {
    mod bar {
        pub fn run() {
            warn!("[bar] warn");
            info!("[bar] info");
            debug!("[bar] debug");
        }
    }

    pub fn run() {
        warn!("[foo] warn");
        info!("[foo] info");
        debug!("[foo] debug");
        bar::run();
    }
}

fn main() {
    env_logger::init();
    warn!("[root] warn");
    info!("[root] info");
    debug!("[root] debug");
    foo::run();
}

RUST_LOG环境变量,控制[env_logger][env_logger]输出。模块声明采用逗号分隔项,格式如下path::to::module=log_level。 运行test应用如下:

RUST_LOG="warn,test::foo=info,test::foo::bar=debug" ./test

设置默认值log::Levelwarn,而模块foo和模块foo::bar则分别为infodebug

WARN:test: [root] warn
WARN:test::foo: [foo] warn
INFO:test::foo: [foo] info
WARN:test::foo::bar: [bar] warn
INFO:test::foo::bar: [bar] info
DEBUG:test::foo::bar: [bar] debug