sezaru

sezaru

Sometimes I get some errors in my log that doesn’t have enough information to allow me to reproduce the issue.

So I was wondering how can I add custom metadata to a process so when a new error is logged, that metadata is show too.

I know that you can do something like this with Logger.metadata, but AFAIK, this only works with structured data, basically you define in the config the keys you accept as metadata and that is what I you can use in all your project.

What I want is to be able to add custom data depending on what I’m doing. For example, if I’m processing some data from a crypto market like BTC_USDT, then I would like my log to show something like market: BTC_USDT when an error is logged.

In other words, I don’t want to have to fill the metadata config with a bunch of keys for every time I want to add a new metadata in my codebase.

config :logger, :console,
 metadata: [...]

Showing Posts 1 to 2

sezaru

sezaru OP

I think I figured it out.

What I did was add a :other field to my metadata array, then i adde a case for my custom log formatter to handle that field.

So my config became like this:

config :logger, :console,
  level: :debug,
  metadata: [:other],
  format: {Log.Formatter, :format}

And my formatter like this:

defmodule Log.Formatter do
  @moduledoc false

  alias IO.ANSI

  @color_reset ANSI.reset()
  @highlight ANSI.light_magenta()

  def format(:debug, message, timestamp, metadata),
    do: format(:debug, message, timestamp, metadata, ANSI.cyan())

  def format(:info, message, timestamp, metadata),
    do: format(:info, message, timestamp, metadata, ANSI.green())

  def format(:notice, message, timestamp, metadata),
    do: format(:notice, message, timestamp, metadata, ANSI.light_yellow())

  def format(:warn, message, timestamp, metadata),
    do: format(:warn, message, timestamp, metadata, ANSI.yellow())

  def format(:warning, message, timestamp, metadata),
    do: format(:warn, message, timestamp, metadata, ANSI.yellow())

  def format(:error, message, timestamp, metadata),
    do: format(:error, message, timestamp, metadata, ANSI.red())

  def format(:critical, message, timestamp, metadata),
    do: format(:critical, message, timestamp, metadata, ANSI.yellow_background())

  def format(:alert, message, timestamp, metadata),
    do: format(:alert, message, timestamp, metadata, ANSI.light_red_background())

  def format(:emergency, message, timestamp, metadata),
    do: format(:emergency, message, timestamp, metadata, ANSI.red_background())

  defp format(level, message, timestamp, metadata, color) do
    date_time = format_date_time(timestamp)
    metadata = format_metadata(metadata, color)

    "\n#{color}#{date_time} [#{level}] #{metadata}\n↳ #{message}#{@color_reset}\n"
  rescue
    _ -> "Could not format message: #{inspect({level, message, timestamp, metadata})}"
  end

  # This is the function I added
  defp format_metadata([{:other, value} | rest], color) do
    "#{@highlight}#{inspect(value)}#{color} " <> format_metadata(rest, color)
  end

  defp format_metadata([{_key, value} | rest], color) do
    "#{inspect(value)} " <> format_metadata(rest, color)
  end

  defp format_metadata([], _), do: ""

  defp format_date_time({date, time}) do
    alias Logger.Formatter

    "#{Formatter.format_date(date)} #{Formatter.format_time(time)}"
  end
end

That way, I can send any data to it using the other metadata,

For example, Logger.error("blibs", other: %{blibs: 2}) will be logged as:

2025-07-14 19:23:44.530 [error] #PID<0.770.0> %{blibs: 2} 
↳ blibs
axelson

axelson

Scenic Core Team

You can also set metadata: :all to log whatever metadata you pass.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews