diff --git a/lib/tesla/middleware/meta_logger.ex b/lib/tesla/middleware/meta_logger.ex index 949d9b1..8627b37 100644 --- a/lib/tesla/middleware/meta_logger.ex +++ b/lib/tesla/middleware/meta_logger.ex @@ -55,12 +55,13 @@ if Code.ensure_loaded?(Tesla) do @impl true def call(%Env{} = env, next, options) do + caller_metadata = MetaLogger.metadata() options = prepare_options(options, env.opts) env - |> log_request(options) + |> log_request(options, caller_metadata) |> Tesla.run(next) - |> log_response(options) + |> log_response(options, caller_metadata) end @spec prepare_options(Env.opts(), Env.opts()) :: Env.opts() @@ -81,36 +82,46 @@ if Code.ensure_loaded?(Tesla) do defp maybe_put_default_value(options, key, default_value), do: Keyword.put(options, key, Keyword.get(options, key, default_value)) - @spec log_request(Env.t(), Env.opts()) :: Env.t() - defp log_request(%Env{} = env, options) do + @spec log_request(Env.t(), Env.opts(), Keyword.t()) :: Env.t() + defp log_request(%Env{} = env, options, caller_metadata) do + MetaLogger.log(:debug, "Tesla.Middleware.log_request") + MetaLogger.log(:debug, "Tesla.Middleware.log_request", caller_metadata) + method = format_method(env.method) url = build_url(env.url, env.query, options) headers = build_headers(env.headers, options) body = build_body(env.body, options) level = Keyword.get(options, :log_level) - log([method, url, headers], level, options) - log(body, level, options) + log([method, url, headers], level, options, caller_metadata) + log(body, level, options, caller_metadata) env end - @spec log_response(Env.result(), Env.opts()) :: Env.result() - defp log_response({:ok, %Env{} = env} = result, options) do + @spec log_response(Env.result(), Env.opts(), Keyword.t()) :: Env.result() + defp log_response({:ok, %Env{} = env} = result, options, caller_metadata) do + MetaLogger.log( + :debug, + "Tesla.Middleware.log_response Logger.metadata() -> #{inspect(Logger.metadata())}" + ) + + MetaLogger.log(:debug, "Tesla.Middleware.log_response", caller_metadata) + headers = build_headers(env.headers, options) body = build_body(env.body, options) level = response_log_level(result, options) - log([env.status, headers], level, options) - log(body, level, options) + log([env.status, headers], level, options, caller_metadata) + log(body, level, options, caller_metadata) result end - defp log_response({:error, reason} = result, options) do + defp log_response({:error, reason} = result, options, caller_metadata) do level = response_log_level(result, options) - log(reason, level, options) + log(reason, level, options, caller_metadata) result end @@ -171,25 +182,29 @@ if Code.ensure_loaded?(Tesla) do |> String.upcase() end - @spec log(message(), Logger.level(), Env.opts()) :: :ok - defp log(message, _level, _options) when message in @empty_values, do: :ok + @spec log(message(), Logger.level(), Env.opts(), Keyword.t()) :: :ok + defp log(message, _level, _options, _caller_metadata) when message in @empty_values, do: :ok - defp log(message, level, options) when is_list(message) do + defp log(message, level, options, caller_metadata) when is_list(message) do message |> Enum.join(" ") - |> log(level, options) + |> log(level, options, caller_metadata) end - defp log(message, level, options) when is_binary(message) do + defp log(message, level, options, caller_metadata) when is_binary(message) do max_entry_length = Keyword.get(options, :max_entry_length) + MetaLogger.log(:debug, "options ->" <> inspect(options), caller_metadata) + MetaLogger.log(:debug, "metadata -> " <> inspect(caller_metadata), caller_metadata) + message |> Slicer.slice(max_entry_length) |> Enum.map(&prepend_tag(&1, options)) - |> Enum.each(&MetaLogger.log(level, &1)) + |> Enum.each(&MetaLogger.log(level, &1, caller_metadata)) end - defp log(message, level, options), do: log(inspect(message), level, options) + defp log(message, level, options, caller_metadata), + do: log(inspect(message), level, options, caller_metadata) @spec prepend_tag(String.t(), Env.opts()) :: String.t() defp prepend_tag(message, options) do