sile / sloggers

A Rust library which provides frequently used slog loggers and convenient functions
MIT License
37 stars 18 forks source link

Bug: The loggers are not flushed regularly #29

Closed Kixunil closed 4 years ago

Kixunil commented 4 years ago

Consider this error handling code:

if let Err(error) = work() {
    error!(logger, "The process failed", "error" => ?err);
   std::process::exit(1);
}

This will fail to print the log message because the logs are not flushed. Also, if the program got killed suddenly, the logs would be lost.

Recommended solution: Flush the logs after each written message.

sile commented 4 years ago

Hi @Kixunil,

Your opinion sounds reasonable, but it's intended behavior. I initially developed sloggers to use it for a middleware package (server) that needs to handle a massive number of requests. In such a case, if log messages are flushed every time, there is a risk that the performance of the server may degrade significantly by the synchronous logging. It was not acceptable to me, so I decided to make sloggers write messages asynchronously (using slog-async internally). I think that you might be able to mitigate this problem by setting channel_size to 0 and overflow_strategy to Block (it's not a perfect solution though). It may be beneficial to provide an option to disable the asynchronous mode and switch to synchronous logging (I don't have enough time to implement this feature, but welcome any contributions if you're interested).

Kixunil commented 4 years ago

Ah, I see. I figured out that dropping the logger just before exiting helps in the specific case I mentioned. Fortunately, Rust code doesn't tend to be killed by stuff like SIGSEGV, so maybe not too huge issue. :)

Since my specific need can be reasonably solved more simply, I'm not too motivated to create full-blown solution either, but I will make a documentation PR to help others avoid the frustration figuring it out, that I had.