diff --git a/config/config.exs b/config/config.exs index 68a81ab..91e6b3b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,3 +1,3 @@ import Config -config :logger, :default_formatter, metadata: ~w(bar baz foo)a +config :logger, :default_formatter, metadata: ~w(bar baz foo request_id)a diff --git a/test/tesla/middleware/meta_logger_test.exs b/test/tesla/middleware/meta_logger_test.exs index 8db95d5..884087a 100644 --- a/test/tesla/middleware/meta_logger_test.exs +++ b/test/tesla/middleware/meta_logger_test.exs @@ -1,5 +1,7 @@ defmodule Tesla.Middleware.MetaLoggerTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false + + require Logger import ExUnit.CaptureLog @@ -227,6 +229,23 @@ defmodule Tesla.Middleware.MetaLoggerTest do assert logs =~ "[debug] [#{inspect(Subject)}] #{String.duplicate("b", 100)}\n" end + test "when the max entry length is given and the logs are split, the metadata is captured with each line" do + Logger.metadata(request_id: "123123123") + + body = String.duplicate("x", 100) + + log_lines = + capture_log(fn -> + FakeClient.post("/huge-response", body, opts: [max_entry_length: 10]) + end) + |> String.split("\n\n") + + log_lines + |> Enum.each(fn log_line -> + assert log_line =~ "request_id=123123123" + end) + end + test "when response is an error, logs the response with error log level" do logs = capture_log(fn -> FakeClient.get("/error") end)