Closed Kixunil closed 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).
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.
Consider this error handling code:
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.