Gladear
Can a `Logger.with_metadata/2` be useful?
Hello ![]()
I’ve been looking for a way to do, put simply:
# Set metadata
Logger.metadata(meta1: :one)
# Execute code
# Remove metadata
Logger.metadata(meta1: nil)
I could have used this multiple times now. This method works fine, but when the `# Execute code` starts getting bigger, it’s easy to forget resetting the metadata at the end of it. Also, if an exception arises in the code and is caught, the metadata won’t get reset.
So I was wondering if a function `Logger.with_metadata([meta1: :one], fn → … end)` could be useful to integrate in `Logger`. Any thought about it? ![]()
Most Liked
hauleth
I do not think that it would be useful to have in core, but as it is not that hard to implement on your own, then I do not think that it would be much of a problem.
I never had need for such thing, so I may be biased there.
dimitarvp
I basically rolled this out for myself during three previous consulting engagements. I suggest you do the same. No point having such a surface-level helper upstreamed. Do it like f.ex. Repo.transaction – you pass a function / MFA to your helper and then do the cleanup after it is executed.
hauleth
The problem I see there is that there is no clear semantic of how it should work. That is why it should be left up to the user.
Imagine situation like:
# metadata = %{foo: :bar}
Logger.with_metadata([foo: :baz], fn ->
Logger.metadata(foo: :quux)
end)
# What should be the value of `:foo` key in metadata?
You say that
But if exception is raised, then system is in broken state anyway. So caller should know whether they need to reset the state to known form, not the callee.
That is why there is no clear way forward with that, as there is too many corner cases and the usability of such function is IMHO low, and implementing what you need is quite simple:
%{foo: old_foo} = Logger.metadata()
Logger.metadata(foo: :new_foo)
try do
# Code
after
Logger.metadata(foo: old_foo)
end
Last Post!
Gladear
Popular in Proposals: Ideas
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
- #code-sync
- #javascript
- #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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









