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.

Schultzer

Schultzer

Yeah, Logger has some sharp edges, but I also believe this might not be the right tool for the job, I’m a big fan of telemetry for these kind of things, even thought structured logs has come to the BEAM, seams like a lot of setup for something that should be simpler, but there might be good reasons for it.

dimitarvp

dimitarvp

Same here for OTel, that’s going to be my next step, I even experimented with OpenObserve in my Golang and Rust work and it worked amazingly well there – logs, metrics and telemetry, all included – so I’ll do it with Elixir next.

Indeed Logger itself seems like it’s not getting the love it deserves and to me it looks more like a thin wrapper over Erlang’s :logger that does not attempt to fix its sharp edges and just carries them over.

Which is fine but it’s IMO dooming it to obscurity.

Good reasons or not, technology should be ergonomic. If it’s not then it’s not living up to its potential. And I quite agree with your “a lot of setup for something that should be simpler”, yep.

Where Next?

Popular in Questions 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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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

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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New

We're in Beta

About us Mission Statement