Skip to content

[feature request] Support special metadata keysΒ #164

@benjreinhart

Description

@benjreinhart

Hello and thank you for this library! However, it seems that logger_json does not support the special metadata keys (or maybe a bug, not sure):

There are two special metadata keys, :module and :function, which extract the relevant bits from :mfa.

We would like to make use of the special module key, but it does not seem to work when using the Basic formatter. This is useful to us as we can use this to key off of in our observability tools with an exact match, rather than e.g. supply it manually or use :mfa and then match on starts_with/contains (less exact, can capture submodules).

πŸ™πŸ» πŸ™πŸ»

Example

If I have a module MyApp.Foo.Bar with some calls to eg Logger.info and I use this configuration:

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

Then I see my logs contain a module=MyApp.Foo.Bar.

If I use this configuration:

config :logger,
  default_handler: [
    formatter: {LoggerJSON.Formatters.Basic, metadata: [:request_id, :module]}
  ]

I do not.

update

One other thing to note using the same configs above: If I explicitly add module metadata, the logger_json formatter will use the fully qualified module (ie Elixir.) whereas the other one will not.

defmodule MyApp.Foo.Bar do
  require Logger
  def log do
    Logger.info("log", module: __MODULE__)
  end
end
iex(6)> MyApp.Foo.Bar.log
{"message":"log","time":"2025-10-09T17:52:36.027Z","metadata":{"module":"Elixir.MyApp.Foo.Bar"},"severity":"info"}

vs

iex(2)> MyApp.Foo.Bar.log
[info] module=MyApp.Foo.Bar  log

It would be great if the behavior of logger_json could match that

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions