emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
315 stars 55 forks source link

Keeping the logger handle alive #135

Closed xkr47 closed 1 year ago

xkr47 commented 1 year ago

Regarding keeping the logger handle alive — you could add to the documentation¹ that you can also use:

let handle = Logger::try_with...().start()?;

std::mem::forget(handle)

This lets the handle stay alive until the end of the program. So one doesn't need to keep the handle stored anywhere.

¹) at least in Logger::start() russtdoc and in https://docs.rs/flexi_logger/latest/flexi_logger/error_info/index.html#write

emabee commented 1 year ago

When being dropped, the logger handle flushes all not yet written log lines from the buffers to the target. If you use std::mem::forget(handle), drop() will never be called and thus flushing will not happen when the program terminates. You then run the risk that the last few log lines do not appear.