env_logger icon indicating copy to clipboard operation
env_logger copied to clipboard

Add example for millisecond since init

Open vigna opened this issue 9 months ago • 4 comments

Log4J has this nice feature that part of the log record is the number of millisecond since the logger initialization. It makes for a very simple timer giving you an idea of how long the process has run. Would it be possible to get it in env_logger?

vigna avatar Mar 27 '25 14:03 vigna

I suspect this could be implemented with a custom formatter (env_logger::Builder::format). Would you be willing to experiment with that to see what lessons could be learned in how well this works and generalizes?

epage avatar Mar 27 '25 15:03 epage

We already have a custom formatter. But to obtain that result we would need to store in a OnceLock or similar the start, and accessing the OnceLock would add further contention.

vigna avatar Mar 27 '25 15:03 vigna

Ok, sorry, we just found that we can do an Instant::now() and move the value inside the closure. So it's entirely doable. Maybe it would be interesting as an example.

vigna avatar Mar 28 '25 10:03 vigna

Re-opening to track someone writing that example.

epage avatar Mar 28 '25 14:03 epage