From 9a2decfd9acd36d5735c39126dc79b39aa0bdf89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Perttu=20K=C3=A4rn=C3=A4?= Date: Mon, 14 Oct 2024 10:25:42 +0300 Subject: [PATCH] Include file, line and mfa in basic formatter metadata --- lib/logger_json/formatter/metadata.ex | 2 +- lib/logger_json/formatters/basic.ex | 3 +-- test/logger_json/formatter/metadata_test.exs | 1 - test/logger_json/formatters/basic_test.exs | 18 ++++++++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/logger_json/formatter/metadata.ex b/lib/logger_json/formatter/metadata.ex index 2345ea3..b34135a 100644 --- a/lib/logger_json/formatter/metadata.ex +++ b/lib/logger_json/formatter/metadata.ex @@ -1,7 +1,7 @@ defmodule LoggerJSON.Formatter.Metadata do @moduledoc false - @ignored_metadata_keys ~w[ansi_color initial_call crash_reason pid gl mfa report_cb time]a + @ignored_metadata_keys ~w[ansi_color initial_call crash_reason pid gl report_cb time]a @doc """ Takes current metadata option value and updates it to exclude the given keys. diff --git a/lib/logger_json/formatters/basic.ex b/lib/logger_json/formatters/basic.ex index f14faf7..bccb672 100644 --- a/lib/logger_json/formatters/basic.ex +++ b/lib/logger_json/formatters/basic.ex @@ -19,8 +19,7 @@ defmodule LoggerJSON.Formatters.Basic do @behaviour LoggerJSON.Formatter - @processed_metadata_keys ~w[file line mfa - otel_span_id span_id + @processed_metadata_keys ~w[otel_span_id span_id otel_trace_id trace_id conn]a diff --git a/test/logger_json/formatter/metadata_test.exs b/test/logger_json/formatter/metadata_test.exs index 1ee3576..6b4ae06 100644 --- a/test/logger_json/formatter/metadata_test.exs +++ b/test/logger_json/formatter/metadata_test.exs @@ -99,7 +99,6 @@ defmodule LoggerJSON.Formatter.MetadataTest do crash_reason: "crash_reason", pid: "pid", gl: "gl", - mfa: "mfa", report_cb: "report_cb", time: "time" } diff --git a/test/logger_json/formatters/basic_test.exs b/test/logger_json/formatters/basic_test.exs index ffc2243..f561d8f 100644 --- a/test/logger_json/formatters/basic_test.exs +++ b/test/logger_json/formatters/basic_test.exs @@ -114,6 +114,24 @@ defmodule LoggerJSON.Formatters.BasicTest do assert log["trace"] == "294740ce41cc9f202dedb563db123532" end + test "logs file, line and mfa as metadata" do + metadata = + capture_log(fn -> + Logger.debug("Hello") + end) + |> decode_or_print_error() + |> Map.get("metadata") + + assert metadata |> Map.get("file") |> to_string() =~ "logger_json/formatters/basic_test.exs" + assert metadata |> Map.get("line") |> is_integer() + + assert metadata["mfa"] === [ + "Elixir.LoggerJSON.Formatters.BasicTest", + "test logs file, line and mfa as metadata", + 1 + ] + end + test "logs metadata" do Logger.metadata( date: Date.utc_today(),