Trouble getting logger metadata in error reports

Even though my question uses Elixir, I think this might be more of an Erlang question since the Elixir’s Logger.Translator is not getting any metadata in error reports.

Question

How to get metadata passed to error reports?

For example, if my process has metadata set:

Logger.configure(metadata: :all)

spawn(fn ->
  Logger.metadata(user_id: "123")
  raise "oops"
end)

I want to get user_id to be added to the error report. Right now it’s not being added.

What happens?

Here’s the report that I get:

08:27:31.269 erl_level=error node=nonode@nohost pid=<0.370.0> Process #PID<0.370.0> raised an exception
** (RuntimeError) oops
  ... stacktrace

Tracing Logger.Translator shows that it’s not getting user_id or any other custom metadata from :logger_proxy:

# 12:27:31 #PID<0.71.0> (:logger_proxy)
# Logger.Translator.translate(:debug, :error, :format, {'Error in process ~p with exit value:~n~p~n', [#PID<0.370.0>, {%RuntimeError{message: "oops"}, [{E, :"-crash/0-fun-0-", 0, [file: 'lib/e.ex', line: 22, error_info: %{module: Exception}]}]}]})

What do I expect?

I expect the metadata to be added to the error report:

note the added user_id

08:27:31.269 user_id=123 erl_level=error node=nonode@nohost pid=<0.370.0> Process #PID<0.370.0> raised an exception
** (RuntimeError) oops
  ... stacktrace
1 Like

proc_lib processes started with Task do include additional metadata.

Task.start_link(fn ->
  Logger.metadata(user_id: "123")
  raise "oops"
end)
09:22:42.715 erl_level=error domain=otp.elixir node=nonode@nohost pid=<0.508.0> user_id=123 Task #PID<0.508.0> started from #PID<0.454.0> terminating
** (RuntimeError) oops
    (e 0.1.0) lib/e.ex:19: anonymous fn/0 in E.crash/0
    (elixir 1.14.0) lib/task/supervised.ex:89: Task.Supervised.invoke_mfa/2
    (stdlib 4.0.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Function: #Function<0.125245401/0 in E.crash/0>
    Args: []
1 Like