Propagate logger metadata in spawned task

Hi there,
I have a phoenix controller from which I spawn a Task with Task.Supervisor.start_child for some async processing.
I’d like to propagate all the metadata I have in the logger of the parent process to the task logger, in order to have a continuous tracing; is there a better/standard way of doing this rather than setting a parameter on the function

Task.Supervisor.start_child(fn -> my_task(Logger.metadata()) end)

def my_task(metadata) do
  IO.puts(metadata)
end

or setting a variable to be seen from the closure:

my_logger_metadata = Logger.metadata()
Task.Supervisor.start_child(fn -> IO.puts(my_logger_metadata) end)

Thank you

3 Likes

Not really. All approaches will effectively need the same approach. However I would do it like that:

metadata = :logger.get_process_metadata()

Task.Supervisor.start_child(fn ->
  :logger.set_process_metadata(metadata)
  my_task()
end)

So the my_task/0 will not need to know anything about logger metadata.

The reason why I use :logger API instead of Logger API is that using :logger directly do not require to change map to list and then list to map and instead just uses map directly.

9 Likes

Thanks for the feedback!

I recently observed a strange error in some code using this pattern:

** (ArgumentError) argument error
logger.erl:621 :logger.set_process_metadata(:undefined)

I’m not familiar with Erlang but looking at the docs, it looks like it might not be technically safe because get_process_metadata can return :undefined whereas set_process_metadata only accepts a metadata map?

I mean it returns that if no process meta data is set, so if you always make sure to set it you should be save - which of course is more complex in a bigger data.

As an alternative you can use Logger.metadata/0 - which seems to return [] in this case and hence shouldn’t break.

iex(1)> :logger.get_process_metadata()
:undefined
iex(2)> Logger.metadata()
[]