dimitarvp
How can I see log metadata in dev?
EDIT: There has been back and forth and I was impatient, operating under the assumption that I am missing something obvious and small. Turns out it wasn’t exactly that. I accepted @LostKobrakai’s comment because it showed me how exactly to configure Logger metadata at runtime, but it was @ibarch’s comment near the end that finally made it work (because apparently Logger ignores “complex” metadata so you have to either do Jason.encode!(metadata) or inspect(metadata).
I have failed the 5-minute test of finding what I need. ![]()
So I have this code somewhere in my app:
Logger.error("Invalid payload", payload: message)
I copied my default Logger config to config/dev.exs:
config :logger, :console,
format: "$time $level $metadata $message\n",
metadata: [:request_id]
So I call the function with an invalid parameter and all I see is this:
15:54:12.439 error Invalid payload
No metadata. Tried with info just in case, same result. What should be changed?
Marked As Solved
LostKobrakai
What you described is exatly the reason. The issue here is simply the chick-egg dance of starting a beam VM:
Basically all code on the beam kinda wants the the logger to be running. Therefore it is started as soon as possible by the :kernel application, which is always the first application to start on an the beam (even directly bootstrapped by the vm iirc, see https://www.youtube.com/watch?v=_AqmxltiV9I). So at that point where :kernel is starting the logger it needs to setup the default handler and its formatting. Essentially no custom code has run yet. I’m not even sure config/runtime.exs is executed early enough in an release startup to configure the logger.
Therefore setting configuration with Mix.install doesn’t help. At that point the default handler is already running and configured from the app env. One would need to use the runtime APIs to adjust the formatter config, as there’s no code watching the app env for changes.
:logger.update_formatter_config(:default, %{metadata: [:payload, :application]})
(use :livebook_gl_handler instead of :default in livebook)
Also Liked
ibarch
How does your payload look like? It seems like console logger ignores complex data structures.
config :logger,
default_formatter: [
format: "$metadata[$level] $message\n",
metadata: [:request_id, :primitive, :complex]
]
iex> require Logger
Logger
iex> Logger.error("message", primitive: :foo, complex: %{foo: :bar})
primitive=foo [error] message
iex> Logger.error("message", primitive: :foo, complex: :bar)
primitive=foo complex=bar [error] message
iex> Logger.error("message", primitive: :foo, complex: Jason.encode!(%{foo: :bar}))
primitive=foo complex={"foo":"bar"} [error] message
linusdm
That’s to be expected. You can add metadata that is associated with the current process, using Logger.metadata/1. Whatever you pass in with Logger.error/2 as a second parameter will add to that metadata. But that “base” metadata can be anything, and by default is probably rightfully so an empty list.
dimitarvp
OK I am definitely having a Saturday moment and spoke too soon without scanning properly. The application key got printed, the payload thing didn’t. ![]()
Still leaving @LostKobrakai’s comment as the solution since it achieved partial success but the search goes on.
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
- #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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









