jdobrzanski
Get "Last Message" during rescue inside a GenServer
There is a very useful error log that prints if I don’t recuse an exception from within a GenServer.
GenServer {[GENSERVER NAME HERE]} terminating
...
Last message (from #PID[PID HERE]): [ARGS TO THE HANDLE CALL HERE]
State: [GENSERVER STATE HERE]
I’ve dug around the elixir code base but can’t find how {'** Generic server ' ++ _, [name, last, state, reason | client]} is getting set in elixir/lib/logger/lib/logger.ex. Most importantly I want last (which is [ARGS TO THE HANDLE CALL HERE]) for my own logging purposes. Hoping someone has a way to access this in the recuse block of a try and not an answer that involves somehow storing the message args from the handle_call function…
Most Liked
hauleth
There is no other way than storing that message on your own somehow. If you want to show this as a part of log message then you put that information into logger metadata.
The gen_server loop in highly simplified form does exactly that (in Elixir for clarity):
def loop(state) do
msg =
receive do
message -> message
end
new_state =
try do
handle_message(message, state)
catch
kind, value ->
:logger.error(%{last_msg: msg, kind: kind, value: value})
:erlang.raise(kind, value, __STACKTRACE__)
end
loop(new_state)
end







