Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 1.1 KB

log-env-variable.zh.md

File metadata and controls

32 lines (24 loc) · 1.1 KB

使用自定义环境变量,设置日志记录

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

Builder配置日志记录。

Builder::parse解析MY_APP_LOG环境变量内容,变为RUST_LOG语法形式。然后,Builder::init初始化记录器。所有这些步骤,通常都是由env_logger::init内部完成。

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

use std::env;
use env_logger::Builder;

fn main() {
    Builder::new()
        .parse(&env::var("MY_APP_LOG").unwrap_or_default())
        .init();

    info!("informational message");
    warn!("warning message");
    error!("this is an error {}", "message");
}