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