Example of using Logger.metadata

Given the following entry in your config file:

config :logger, :console, format: "[$level] $metadata$message\n",
  metadata: [:module, :function, :my_id]

You can do the following;

iex(1)> require Logger
Logger
iex(2)> Logger.info "test"
[info] module= function= test
:ok
iex(3)> Logger.info "test", [my_id: 123]
[info] module= function= my_id=123 test
:ok
iex(4)> Logger.metadata [my_id: 999]
:ok
iex(5)> Logger.info "another test"
[info] module= function= my_id=999 another test
:ok
iex(6)>

I added the :module and :function into the meta data, as an example of other meta data you might want. However, since I’m running it from iex, the don’t print anything useful.

9 Likes