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 Responses
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








