Skip to content

Commit

Permalink
Update Tesla.Middleware.Logger for Elixir 1.11+ (#627)
Browse files Browse the repository at this point in the history
Elixir 1.15 is now warning whenever the `:warn` level is used with the
recommendation of using `:warning` instead. The `Logger` middleware uses
`Logger.log/3`, so a compile-time warning would not be shown, only
a runtime warning.

This change adds `:warning` as a valid configuration option for Tesla
log level and automatically translates `:warn` or `:warning` to the
appropriate level (`:warn` for Elixir 1.10.x, `:warning` for Elixir
1.11.x or higher, as `Logger.warning/2` was introduced in 1.11.0).
  • Loading branch information
halostatue authored Oct 19, 2023
1 parent 857d7da commit 1dd7992
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/tesla/middleware/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ defmodule Tesla.Middleware.Logger do
By default, the following log levels will be used:
- `:error` - for errors, 5xx and 4xx responses
- `:warn` - for 3xx responses
- `:warn` or `:warning` - for 3xx responses
- `:info` - for 2xx responses
You can customize this setting by providing your own `log_level/1` function:
Expand Down Expand Up @@ -186,7 +186,13 @@ defmodule Tesla.Middleware.Logger do

@format Formatter.compile(@config[:format])

@type log_level :: :info | :warn | :error
@type log_level :: :info | :warn | :warning | :error

if Version.compare(System.version(), "1.11.0") == :lt do
@warning_level :warn
else
@warning_level :warning
end

require Logger

Expand Down Expand Up @@ -221,9 +227,13 @@ defmodule Tesla.Middleware.Logger do
fun when is_function(fun) ->
case fun.(env) do
:default -> default_log_level(env)
warning when warning in [:warn, :warning] -> @warning_level
level -> level
end

warning when warning in [:warn, :warning] ->
@warning_level

atom when is_atom(atom) ->
atom
end
Expand All @@ -233,7 +243,7 @@ defmodule Tesla.Middleware.Logger do
def default_log_level(env) do
cond do
env.status >= 400 -> :error
env.status >= 300 -> :warn
env.status >= 300 -> @warning_level
true -> :info
end
end
Expand Down

0 comments on commit 1dd7992

Please sign in to comment.