japhib
Docs say not to use Logger.flush/0 in production - what's the downside?
We’re in the process of adding additional error logging for unhandled exceptions in a Phoenix application. One of the issues we’re experiencing is that because the unhandled exception is crashing the request process, we sometimes lose unflushed Logger messages.
To solve this, I’d like to use Logger.flush/0. However, the docs say:
This is useful for testing and it should not be called in production code.
What exactly is the downside of calling this in production code?
We really want our logs to be flushed in these error cases, and we’re okay with the process blocking for a bit until that happens. But is there some other downside of this function that would make this undesirable to call in our case?
Marked As Solved
josevalim
If your process crashes, it will be logged anyway, Logger.flush won’t make any difference. Except if there are too many log entries, then the logger may discard some, but Logger.flush won’t change it.
Also Liked
josevalim
Sorry, I missed this. I have already updated the docs. ![]()
josevalim
The reason why it is not encouraged is because it tells all loggers to stop what they are doing and write to disk. If you call it several times, they will stop and write to disk, every single time, even though they believe batching and waiting is the efficient thing to do.
josevalim
Of course, we can always improve the docs and PRs are welcome. If this is happening quite a bit, then we are not being told about those cases, because they would certainly have been addressed otherwise. ![]()
ruslandoga
![]()
Are you sure this is what’s happening?
I tried crashing a process by raising an exception right after logging and it works as expected:
iex> require Logger
iex> spawn(fn ->
...> Logger.info("info")
...> raise "oops"
...> end)
00:58:27.917 [info] info
00:58:27.919 [error] Process #PID<0.109.0> raised an exception
** (RuntimeError) oops
Maybe your system is getting overloaded?
ruslandoga
Also looking into the actual implementation of Logger.flush/0 it seems to be no-op by default: elixir/lib/logger/lib/logger.ex at 78f63d08313677a680868685701ae79a2459dcc1 · elixir-lang/elixir · GitHub – because the default logging destination for logger_std_h (which Logger uses) is starndard_io and not a file – but I haven’t actually verified it, just took a look at the code ![]()







