Skip to content

Commit

Permalink
Merge pull request #117 from KodrAus/fix/retain-previously-set-max-level
Browse files Browse the repository at this point in the history
Don't clobber a previously set level filter
  • Loading branch information
KodrAus committed Nov 11, 2018
2 parents c1f2c20 + 21858cd commit fa6a7df
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
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);
}

0 comments on commit fa6a7df

Please sign in to comment.