Skip to content

Added example of timer #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@
//! env_logger::Builder::from_env(Env::default().default_filter_or("warn")).init();
//! ```
//!
//! ## Using the Logger as a Timer
//!
//! `env_logger` can also be used as a timer by moving an initial
//! timestamp into the closure defining a custom format:
//!
//! ```
//! use std::io::Write;
//!
//! let start = std::time::Instant::now();
//! env_logger::builder()
//! .format(move |buf, record| {
//! writeln!(buf, "{} ({}): {}", record.level(), start.elapsed().as_millis(), record.args())
//! })
//! .init();
//! ```
//!
Comment on lines +259 to +274
Copy link
Contributor

Choose a reason for hiding this comment

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

I was assuming this would be created as another example in examples/. The advantage of this is its easier to interactively play with to see the results.

If its being added to the docs, putting it on Builder::format might be a better approach.

Copy link
Author

Choose a reason for hiding this comment

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

So sorry to disappoint your expectations 😆.

//! [gh-repo-examples]: https://github.com/rust-cli/env_logger/tree/main/examples
//! [level-enum]: https://docs.rs/log/latest/log/enum.Level.html
//! [log-crate-url]: https://docs.rs/log
Expand Down