Skip to content

Commit

Permalink
Merge pull request #86 from asajeffrey/make-env-logger-public
Browse files Browse the repository at this point in the history
Made Logger public.
  • Loading branch information
sfackler authored Jul 11, 2016
2 parents c679ad4 + 4154695 commit a95b888
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ use std::mem;

use log::{Log, LogLevel, LogLevelFilter, LogRecord, SetLoggerError, LogMetadata};

struct Logger {
/// The logger.
pub struct Logger {
directives: Vec<LogDirective>,
filter: Option<Regex>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
Expand Down Expand Up @@ -245,7 +246,15 @@ impl LogBuilder {
/// global logger may only be initialized once. Future initialization
/// attempts will return an error.
pub fn init(&mut self) -> Result<(), SetLoggerError> {
log::set_logger(|max_level| {
let logger = self.build();
max_level.set(logger.filter());
Box::new(logger)
})
}

/// Build an env logger.
pub fn build(&mut self) -> Logger {
if self.directives.is_empty() {
// Adds the default filter if none exist
self.directives.push(LogDirective {
Expand All @@ -262,18 +271,6 @@ impl LogBuilder {
});
}

log::set_logger(|max_level| {
let level = {
let max = self.directives.iter().map(|d| d.level).max();
max.unwrap_or(LogLevelFilter::Off)
};
max_level.set(level);

Box::new(self.build())
})
}

fn build(&mut self) -> Logger {
Logger {
directives: mem::replace(&mut self.directives, Vec::new()),
filter: mem::replace(&mut self.filter, None),
Expand All @@ -283,6 +280,21 @@ impl LogBuilder {
}

impl Logger {
pub fn new() -> Logger {
let mut builder = LogBuilder::new();

if let Ok(s) = env::var("RUST_LOG") {
builder.parse(&s);
}

builder.build()
}

pub fn filter(&self) -> LogLevelFilter {
self.directives.iter()
.map(|d| d.level).max()
.unwrap_or(LogLevelFilter::Off)
}

fn enabled(&self, level: LogLevel, target: &str) -> bool {
// Search for the longest match, the vector is assumed to be pre-sorted.
Expand Down

0 comments on commit a95b888

Please sign in to comment.