Skip to content
29 changes: 21 additions & 8 deletions examples/custom_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ Changing the default logging format.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:

```no_run,shell
$ export MY_LOG_LEVEL = 'info'
$ export MY_LOG_LEVEL='info'
```

Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
or `auto` to enable them:

```no_run,shell
$ export MY_LOG_STYLE=never
```

If you want to control the logging output completely, see the `custom_logger` example.
Expand All @@ -14,20 +21,26 @@ If you want to control the logging output completely, see the `custom_logger` ex
extern crate log;
extern crate env_logger;

use std::env;
use std::io::Write;

use env_logger::{Env, Builder, fmt};

fn init_logger() {
let mut builder = env_logger::Builder::new();
let env = Env::default()
.filter("MY_LOG_LEVEL")
.write_style("MY_LOG_STYLE");

let mut builder = Builder::from_env(env);

// Use a different format for writing log records
builder.format(|buf, record| {
writeln!(buf, "My formatted log: {}", record.args())
});
let mut style = buf.style();
style.set_bg(fmt::Color::Yellow).set_bold(true);

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

writeln!(buf, "My formatted log ({}): {}", timestamp, style.value(record.args()))
});

builder.init();
}
Expand Down
17 changes: 9 additions & 8 deletions examples/custom_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ Using `env_logger` to drive a custom logger.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:

```no_run,shell
$ export MY_LOG_LEVEL = 'info'
$ export MY_LOG_LEVEL='info'
```

Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
or `auto` to enable them:

```no_run,shell
$ export MY_LOG_STYLE=never
```

If you only want to change the way logs are formatted, look at the `custom_format` example.
Expand All @@ -23,13 +30,7 @@ struct MyLogger {
impl MyLogger {
fn new() -> MyLogger {
use env_logger::filter::Builder;
let mut builder = Builder::new();

// Parse a directives string from an environment variable
// This uses the same format as `env_logger::Logger`
if let Ok(ref filter) = std::env::var("MY_LOG_LEVEL") {
builder.parse(filter);
}
let mut builder = Builder::from_env("MY_LOG_LEVEL");

MyLogger {
inner: builder.build()
Expand Down
9 changes: 8 additions & 1 deletion examples/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ Using `env_logger`.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:

```no_run,shell
$ export MY_LOG_LEVEL = 'info'
$ export MY_LOG_LEVEL='info'
```

Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
or `auto` to enable them:

```no_run,shell
$ export MY_LOG_STYLE=never
```
*/

Expand Down
18 changes: 18 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
//! [`Builder::parse`]: struct.Builder.html#method.parse
//! [`Filter::matches`]: struct.Filter.html#method.matches

use std::env;
use std::mem;
use std::fmt;
use log::{Level, LevelFilter, Record, Metadata};
Expand Down Expand Up @@ -185,6 +186,17 @@ impl Builder {
}
}

/// Initializes the filter builder from an environment.
pub fn from_env(env: &str) -> Builder {
let mut builder = Builder::new();

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

builder
}

/// Adds a directive to the filter.
///
/// The given module (if any) will log at most the specified level provided.
Expand Down Expand Up @@ -240,6 +252,12 @@ impl Builder {
}
}

impl Default for Builder {
fn default() -> Self {
Builder::new()
}
}

impl fmt::Debug for Filter {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Filter")
Expand Down
Loading