Skip to content

Commit 0cb3860

Browse files
authored
Merge pull request #46 from sebasmagri/feat/public-color-api
Public color API
2 parents 15482ac + 48944ca commit 0cb3860

6 files changed

Lines changed: 768 additions & 198 deletions

File tree

examples/custom_format.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ Changing the default logging format.
44
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
55
66
```no_run,shell
7-
$ export MY_LOG_LEVEL = 'info'
7+
$ export MY_LOG_LEVEL='info'
8+
```
9+
10+
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11+
or `auto` to enable them:
12+
13+
```no_run,shell
14+
$ export MY_LOG_STYLE=never
815
```
916
1017
If you want to control the logging output completely, see the `custom_logger` example.
@@ -14,20 +21,26 @@ If you want to control the logging output completely, see the `custom_logger` ex
1421
extern crate log;
1522
extern crate env_logger;
1623

17-
use std::env;
1824
use std::io::Write;
1925

26+
use env_logger::{Env, Builder, fmt};
27+
2028
fn init_logger() {
21-
let mut builder = env_logger::Builder::new();
29+
let env = Env::default()
30+
.filter("MY_LOG_LEVEL")
31+
.write_style("MY_LOG_STYLE");
32+
33+
let mut builder = Builder::from_env(env);
2234

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

28-
if let Ok(s) = env::var("MY_LOG_LEVEL") {
29-
builder.parse(&s);
30-
}
40+
let timestamp = buf.timestamp();
41+
42+
writeln!(buf, "My formatted log ({}): {}", timestamp, style.value(record.args()))
43+
});
3144

3245
builder.init();
3346
}

examples/custom_logger.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ Using `env_logger` to drive a custom logger.
44
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
55
66
```no_run,shell
7-
$ export MY_LOG_LEVEL = 'info'
7+
$ export MY_LOG_LEVEL='info'
8+
```
9+
10+
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11+
or `auto` to enable them:
12+
13+
```no_run,shell
14+
$ export MY_LOG_STYLE=never
815
```
916
1017
If you only want to change the way logs are formatted, look at the `custom_format` example.
@@ -23,13 +30,7 @@ struct MyLogger {
2330
impl MyLogger {
2431
fn new() -> MyLogger {
2532
use env_logger::filter::Builder;
26-
let mut builder = Builder::new();
27-
28-
// Parse a directives string from an environment variable
29-
// This uses the same format as `env_logger::Logger`
30-
if let Ok(ref filter) = std::env::var("MY_LOG_LEVEL") {
31-
builder.parse(filter);
32-
}
33+
let mut builder = Builder::from_env("MY_LOG_LEVEL");
3334

3435
MyLogger {
3536
inner: builder.build()

examples/default.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ Using `env_logger`.
44
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
55
66
```no_run,shell
7-
$ export MY_LOG_LEVEL = 'info'
7+
$ export MY_LOG_LEVEL='info'
8+
```
9+
10+
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11+
or `auto` to enable them:
12+
13+
```no_run,shell
14+
$ export MY_LOG_STYLE=never
815
```
916
*/
1017

src/filter/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
//! [`Builder::parse`]: struct.Builder.html#method.parse
6060
//! [`Filter::matches`]: struct.Filter.html#method.matches
6161
62+
use std::env;
6263
use std::mem;
6364
use std::fmt;
6465
use log::{Level, LevelFilter, Record, Metadata};
@@ -185,6 +186,17 @@ impl Builder {
185186
}
186187
}
187188

189+
/// Initializes the filter builder from an environment.
190+
pub fn from_env(env: &str) -> Builder {
191+
let mut builder = Builder::new();
192+
193+
if let Ok(s) = env::var(env) {
194+
builder.parse(&s);
195+
}
196+
197+
builder
198+
}
199+
188200
/// Adds a directive to the filter.
189201
///
190202
/// The given module (if any) will log at most the specified level provided.
@@ -240,6 +252,12 @@ impl Builder {
240252
}
241253
}
242254

255+
impl Default for Builder {
256+
fn default() -> Self {
257+
Builder::new()
258+
}
259+
}
260+
243261
impl fmt::Debug for Filter {
244262
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
245263
f.debug_struct("Filter")

0 commit comments

Comments
 (0)