csuriano23
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
Most Liked
hauleth
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.







