Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't clobber a previously set level filter #117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -33,5 +33,9 @@ harness = false
name = "log-in-log"
harness = false

[[test]]
name = "init-twice-retains-filter"
harness = false

[features]
default = ["termcolor", "atty", "humantime", "regex"]
10 changes: 8 additions & 2 deletions src/lib.rs
Expand Up @@ -646,8 +646,14 @@ impl Builder {
pub fn try_init(&mut self) -> Result<(), SetLoggerError> {
let logger = self.build();

log::set_max_level(logger.filter());
log::set_boxed_logger(Box::new(logger))
let max_level = logger.filter();
let r = log::set_boxed_logger(Box::new(logger));

if r.is_ok() {
log::set_max_level(max_level);
}

r
}

/// Initializes the global logger with the built env logger.
Expand Down
40 changes: 40 additions & 0 deletions tests/init-twice-retains-filter.rs
@@ -0,0 +1,40 @@
extern crate log;
extern crate env_logger;

use std::process;
use std::env;
use std::str;

fn main() {
if env::var("YOU_ARE_TESTING_NOW").is_ok() {
// Init from the env (which should set the max level to `Debug`)
env_logger::init();

assert_eq!(log::LevelFilter::Debug, log::max_level());

// Init again using a different max level
// This shouldn't clobber the level that was previously set
env_logger::Builder::new()
.parse("info")
.try_init()
.unwrap_err();

assert_eq!(log::LevelFilter::Debug, log::max_level());
return
}

let exe = env::current_exe().unwrap();
let out = process::Command::new(exe)
.env("YOU_ARE_TESTING_NOW", "1")
.env("RUST_LOG", "debug")
.output()
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
if out.status.success() {
return
}

println!("test failed: {}", out.status);
println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap());
println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap());
process::exit(1);
}