Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/millis_since_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
Comment thread
epage marked this conversation as resolved.
Including the milliseconds elapsed since the logger was initialized in each
record, similar to `Log4J`'s relative time pattern.

Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:

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

use std::io::Write;
use std::time::Instant;

use env_logger::{Builder, Env};

fn init_logger() {
let env = Env::default().filter("MY_LOG_LEVEL");

let start = Instant::now();
Builder::from_env(env)
.format(move |buf, record| {
let elapsed = start.elapsed().as_millis();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consolidate this example with custom_format.rs?

writeln!(
buf,
"[{elapsed:>6} ms] {}: {}",
record.level(),
record.args()
)
})
.init();
}

fn main() {
init_logger();

log::info!("a log from `MyLogger`");
log::warn!("another log, a moment later");
Comment on lines +35 to +38
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have sleeps or something else to help make the elapsed time more obvious?

}
Loading