Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata to logger #631

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add metadata to logger #631

wants to merge 1 commit into from

Conversation

teamon
Copy link
Member

@teamon teamon commented Oct 27, 2023

wip, testing in progress

@teamon
Copy link
Member Author

teamon commented Oct 27, 2023

@yordis wdyt of this as an idea?

Use `:conceal` request option to conceal sensitive requests.

```
Tesla.get(client, opts: [conceal: true]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I like the idea, the only feedback is about documentation.

We should document in one place all the "reserved" options that have a given intent.

I like it!

lib/tesla/middleware/logger.ex Outdated Show resolved Hide resolved
@yordis
Copy link
Member

yordis commented Aug 1, 2024

Recently, I had to send the data somehow, I ended up doing the following:

defmodule Umbrella.Tesla.Logger do
  def log_level(%Tesla.Env{status: status}) when status in 400..499, do: :warning
  def log_level(_env), do: :default

  def log_formatter(req, {:error, reason}, time) do
    %{
      time: time,
      method: upper_case_method(req),
      url: req.url,
      path: req.opts[:req_url],
      req_body: log_env_body(req),
      reason: inspect(reason)
    }
  end

  def log_formatter(req, {:ok, resp}, time) do
    %{
      time: time,
      status: resp.status,
      method: upper_case_method(req),
      url: req.url,
      path: req.opts[:req_url],
      req_body: log_env_body(req),
      resp_body: log_env_body(resp)
    }
  end

  defp log_env_body(%Tesla.Env{status: status} = env) when status >= 400 do
    inspect(env.body)
  end

  defp log_env_body(_), do: "[REDACTED]"

  defp upper_case_method(%Tesla.Env{} = env) do
    env.method
    |> to_string()
    |> String.upcase()
  end
end

As long as the metadata is under a given key tesla: [....] I do not see any issue, although, I still believe that structured logging is much better.

Being said, the ultimate way to deal with this is to simple allow the user to do Logger calls themselves and give them enough information to know what to do with it.

I think it was a misdirection to call Logger ourselves. Except for the debug flag!

@yordis yordis force-pushed the master branch 6 times, most recently from 2bca420 to fe7207c Compare October 24, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants