wfgilman

wfgilman

Example of using Logger.metadata

Can someone provide an example of how you would attach metadata to a process using metatdata/1? If I have a GenServer say that accepts messages with a user_id. Where would I add the user_id to the metadata so that it’s printed in the logging? I’ve been searching the docs and online, but feel I need an example to drive home the concept.

Marked As Solved

smpallen99

smpallen99

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.

Also Liked

smpallen99

smpallen99

@minhajuddin That is a good point. Glad you emphasis that.

It does, however, lend itself to a model where you have a process for each item in some collection. By setting metadata for that process at startup (presumedly with some id or name) its an easy way to identify the logs for the given item of the collection.

Exadra37

Exadra37

To log this I use in runtime.exs:

config :logger, :default_formatter,
# 18:29:12.987 | error | module=MyApp.Some.Module function=some_function/arity line=some_number  | some message here
  format: "$time | $level | $metadata | $message\n",
  metadata: [
    :request_id,
    :module,
    :function,
    :line
  ]
wfgilman

wfgilman

This is exactly what I was looking for. Thank you!

Last Post!

Exadra37

Exadra37

To improve the logger metadata I use this function:

def enhance_metadata(%{} = metadata) do
    context_hash = :crypto.hash(:md5, inspect(metadata)) |> Base.encode16()

    %{
      log_id: UUIDv7.generate(),
      context_hash: context_hash,
      context: metadata
    }
  end

And the updated config:

config :logger, :default_formatter,
  # 22:44:55.512 |> error |> "some good reason" |> log_id=0199e4e5-e458-78d8-b623-31539c4db8b5 context_hash=4C0CAE343D9B60CADC8BD53D5BB84563 module=EmCanvas.Canvas.Items.AllPositions.ListAllItemsPositions function=list/1 line=18
  format: "$time |> $level |> $message |> $metadata\n",
  metadata: [
    :request_id,
    :log_id,
    :context_hash,
    :module,
    :function,
    :line
  ]

Where Next?

Popular in Questions Top

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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement