From fc8f269a1cb0f186972e062397e381056fd07c35 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Wed, 18 Oct 2023 00:15:33 -0400 Subject: [PATCH] Update Tesla.Middleware.Logger for Elixir 1.11+ 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). --- lib/tesla/middleware/logger.ex | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/tesla/middleware/logger.ex b/lib/tesla/middleware/logger.ex index 72f2360a..0db42ae6 100644 --- a/lib/tesla/middleware/logger.ex +++ b/lib/tesla/middleware/logger.ex @@ -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: @@ -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 @@ -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 @@ -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