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:
The issue might be that you only including the request_id in the metadata, Logger — Logger v1.16.2 I believe thats why the payload is excluded and was one of the reason that I scraped including capturing logger in https://dbvisor.com that is leveraging telemetry and storing them in a timeseries table in your DB for easy debugging.
To respond to the second half of your message, production logging and telemetry is yet to be decided but in the meantime I just want to see the metadata on my machine while testing.
If anybody wants to play with this before we solve it, here’s a self-contained script that demonstrates my problem (mine is called logging_playground.exs):
Weird, might be easier to just use telemetry and setup your own handler to print to the console on dev. Then you also gain way more control then Logger could give you.
Maybe, but I’d be hugely surprised if Elixir doesn’t cover this use case trivially. I must be having a Saturday afternoon moment here and missing something extremely obvious.
FWIW, I think your original problem is solved by adding :payload to the list of metadata values. I tried in a new mix project (with mix new), and the log output contained the payload as expected.
I think the Mix.install/2 version doesn’t work, because the Logger config you’re trying to set is part of the boot configuration (which is different from the compile configuration). Both can be set in config/config.exs, but I don’t know how that works with the Mix.install/2 way of configuring things. Maybe that’s just too late to influence the Logger’s boot configuration.
I’ve actually never thought about this “boot configuration” aspect of an Elixir app. So if anyone can chime in… Please do!
PS: just to be clear, this is the config that works, defined in config/config.exs:
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.
But from within one of my modules I still can’t make it work (the format is respected btw, no more square brackets etc. but no metadata). Any suggestions? I should probably start by IO.inspect-ing Logger.metadata(), I suppose.
There is not much to inspect, I’m afraid. Whatever you pass in as a keyword list with the Logger call will be in your metadata.
Is this a Phoenix app? Are you running with mix, and is your mix-env set to dev?
Does it behave differently when you call that module function from iex, as opposed to from within your application?
Running :logger.get_config() reveals the current config. Somewhere in that datastructure I find the metadata and format keys, which should reflect your config.
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.
Hrm, I just used @LostKobrakai’s recommendation and put this in my application.ex after loading config. We have a bespoke mechanism of loading stuff and it’s heavily dependent on the way we deploy apps so that was probably the reason. And one I admit I don’t care much to pursue.
So for posterity for both of you and future readers, do this if you have my problem:
defmodule YourApp.Application do
use Application
require Logger
@impl Application
def start(_type, _args) do
config = load_config() # our custom stuff.
+ :logger.update_formatter_config(:default, %{metadata: [:payload, :application]})
children = [
# The app's children go here.
]
opts = [strategy: :one_for_one, name: Facteur.Supervisor]
Supervisor.start_link(children, opts)
end
end
And you are on your merry way. (Obviously replace [:payload, :application] with whatever metadata you want to be able to pass to your Logger and for it to be visible.)