Gladear

Gladear

Can a `Logger.with_metadata/2` be useful?

Hello :waving_hand:

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? :blush:

Most Liked

hauleth

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

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

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

Gladear

Thanks for your answers, I’ll stick with our custom doing then :smile:

Have a nice day!

Where Next?

Popular in Proposals: Ideas Top

hst337
Elixir compiler and language specification Purpose of the proposal Elixir language is in mature state and no breaking or heavy changes ar...
New
benkimpel
Background I work at a hedge fund and our traders need highly dynamic UIs (think splitters, tabbed panels, tree lists, enormous data grid...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
altdsoy
Hi there :wave:, I hope everyone is doing well… Tl;Dr: Is there a use case to have Heroicons from an actual hex.pm package instead of G...
New
cortfritz
Each time i use phx.gen I end up adding dark mode to it. I’m offering to add this to the default phx gen templates. This would use ta...
New
Redbaritone
In the current Auth code, the email must change to be valid. This may be true for the two situations the author has in mind: Registering ...
New
cevado
I was reading the EEP-79, and thinking about the poor record support in Elixir(i’ve tried to discuss about that in the forum before). I s...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement