dimitarvp

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

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

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

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

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

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

Still leaving @LostKobrakai’s comment as the solution since it achieved partial success but the search goes on.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
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
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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 43806 214
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement