Search code examples
elixirerlang-otpgen-server

Filter Genserver Termination Logs (translator_inspect_opts?)


What is the best way to filter the logging output of a GenServer termination event? It seems like Elixir.Logger allows for :translator_inspect_opts which provide for some configuration here, but I'm not clear on how to handle the following situation:

The default logging output when a Genserver terminates includes the last message received and the state, like so:

10:06:00.165 [error] GenServer #PID<0.378.0> terminating
** (FunctionClauseError) no function clause matching in 
Module.handle_info/2 path_to_file.ex:30: 
...
Last message: {:DOWN, #Reference<0.1522575658.3956015107.616>, 
:process, #PID<0.295.0>, {:function_clause, [{Module.function, 
:handle_info, [{#Reference<0.1522575658.3968335873.44738>, {:ok, 
#Port<0.84>, #PID<0.363.0>}}]}
State: {%{#Reference<0.1522575658.3956015107.615> => [STATE],   
...

In some cases I would like to omit the message and state entirely from the error log, and in some cases I would like to filter out some of the parameters included in each. How can I handle this situation?


Solution

  • AFAICT, it is not possible through Logger configuration and it in general looks like an XY problem. Your willingness to omit logs on some {:DOWN, _, _, _, _}, state events hints that you actually want to treat them differently. Just do it—ErlangVM is very friendly.

    Implement the following GenServer.handle_info/2 in your GenServer:

    @impl
    def handle_info({:DOWN, ref, what, pid, reason}, state) do
      ...
    end
    

    maybe with different clauses and handle different cases differently.