Customize :file logger metadata

Hi folks,

I was looking for a way to remove the file= prefix of this metadata from my logs. I’d like to just have automatically the path to the file because having the prefix messes up with vscode and I can’t click it to get to the file.

From the docs I can’t see a way to customize the built-in metadata, or create my own :file. Is there a easy way to do? Though it’s not really that important :grin:

There is no way to “create your own :file” but with some hacks you can do it right now:

defmodule MyFilter do
  def strip_file_prefix(log, prefix) do
    %{meta: meta} = log

    new_meta = Map.replace(meta, :file, &String.replace_prefix(&1, prefix, ""))

    %{log | meta: new_meta}
  end
end

:logger.add_primary_filter(:strip_file_prefix, {&MyFilter.strip_file_prefix/2, prefix_to_be_stripped})

One thing to remember, &MyFilter.strip_file_prefix/2 should be remote capture, not local capture.

3 Likes