Skip to content

Commit

Permalink
Merge pull request #105 from dbrgn/log-target
Browse files Browse the repository at this point in the history
Make log target configurable
  • Loading branch information
sfackler committed Apr 18, 2017
2 parents 25e349c + a161734 commit 4195412
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -158,3 +158,20 @@ test tests::it_adds_one ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```

## Configuring log target

By default, `env_logger` logs to stderr. If you want to log to stdout instead,
you can use the `LogBuilder` to change the log target:

```rust
use std::env;
use env_logger::{LogBuilder, LogTarget};

let mut builder = LogBuilder::new();
builder.target(LogTarget::Stdout);
if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.init().unwrap();
```
26 changes: 25 additions & 1 deletion env/src/lib.rs
Expand Up @@ -152,11 +152,19 @@ mod filter;
#[path = "string.rs"]
mod filter;

/// Log target, either stdout or stderr.
#[derive(Debug)]
pub enum LogTarget {
Stdout,
Stderr,
}

/// The logger.
pub struct Logger {
directives: Vec<LogDirective>,
filter: Option<filter::Filter>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
target: LogTarget,
}

/// LogBuilder acts as builder for initializing the Logger.
Expand Down Expand Up @@ -196,6 +204,7 @@ pub struct LogBuilder {
directives: Vec<LogDirective>,
filter: Option<filter::Filter>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
target: LogTarget,
}

impl LogBuilder {
Expand All @@ -208,6 +217,7 @@ impl LogBuilder {
format!("{}:{}: {}", record.level(),
record.location().module_path(), record.args())
}),
target: LogTarget::Stderr,
}
}

Expand Down Expand Up @@ -236,6 +246,14 @@ impl LogBuilder {
self
}

/// Sets the target for the log output.
///
/// Env logger can log to either stdout or stderr. The default is stderr.
pub fn target(&mut self, target: LogTarget) -> &mut Self {
self.target = target;
self
}

/// Parses the directives string in the same form as the RUST_LOG
/// environment variable.
///
Expand Down Expand Up @@ -286,6 +304,7 @@ impl LogBuilder {
directives: mem::replace(&mut self.directives, Vec::new()),
filter: mem::replace(&mut self.filter, None),
format: mem::replace(&mut self.format, Box::new(|_| String::new())),
target: mem::replace(&mut self.target, LogTarget::Stderr),
}
}
}
Expand Down Expand Up @@ -337,7 +356,12 @@ impl Log for Logger {
}
}

let _ = writeln!(&mut io::stderr(), "{}", (self.format)(record));
match self.target {
LogTarget::Stdout => println!("{}", (self.format)(record)),
LogTarget::Stderr => {
let _ = writeln!(&mut io::stderr(), "{}", (self.format)(record));
},
};
}
}

Expand Down

0 comments on commit 4195412

Please sign in to comment.