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

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

1 Like

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 ->

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.


Thanks for the feedback!