Skip to content

Commit

Permalink
More log statements and metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivor committed Oct 2, 2024
1 parent 22cd028 commit 36a812b
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions lib/tesla/middleware/meta_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 36a812b

Please sign in to comment.