Elixir 1.19: Warn on old config of :console Logger and conflicts

This commit is contained in:
Phantasm 2026-01-13 23:28:57 +01:00 • committed by Phantasm
commit aed0eb3333
2 changed files with 115 additions and 54 deletions

View file

@ -419,7 +419,52 @@ defmodule Pleroma.Config.DeprecationWarnings do
@spec check_deprecated_logger_config() :: :ok | :error @spec check_deprecated_logger_config() :: :ok | :error
def check_deprecated_logger_config do def check_deprecated_logger_config do
backend = if Application.get_env(:logger, :backends) do mix_env = Pleroma.Config.get(:env)
config_file_name = "config/#{mix_env}.secret.exs"
config_file = File.read(config_file_name)
config_file =
case config_file do
{:ok, contents} ->
String.split(contents, "\n", trim: true)
|> Enum.reject(fn string -> String.starts_with?(string, "#") end)
|> Enum.join("\n")
_ ->
false
end
declared_deprecated_backend =
if is_binary(config_file) do
String.contains?(config_file, "config :logger, backends") or
String.contains?(config_file, "config :logger,\n backends")
else
nil
end
declared_deprecated_console =
if is_binary(config_file) do
String.contains?(config_file, "config :logger, :console") or
String.contains?(config_file, "config :logger,\n console")
else
nil
end
# We can't use Application.get_env/3, because by default default_formatter is populated
declared_new_console =
if is_binary(config_file) do
String.contains?(config_file, "config :logger, :default_handler") or
String.contains?(config_file, "config :logger,\n :default_handler") or
String.contains?(config_file, "config :logger, :default_formatter") or
String.contains?(config_file, "config :logger,\n :default_formatter")
else
nil
end
use_new_backend_config = Pleroma.Config.get([:logger, :backends])
backend =
if declared_deprecated_backend do
Logger.warning(""" Logger.warning("""
!!!DEPRECATION WARNING!!! !!!DEPRECATION WARNING!!!
Your configuration is using deprecated syntax for configuring backends of Elixir's logger. Your configuration is using deprecated syntax for configuring backends of Elixir's logger.
@ -437,7 +482,8 @@ defmodule Pleroma.Config.DeprecationWarnings do
false false
end end
console = if Application.get_env(:logger, :console) do console =
if declared_deprecated_console do
Logger.warning(""" Logger.warning("""
!!!DEPRECATION WARNING!!! !!!DEPRECATION WARNING!!!
Your configuration is using deprecated syntax for configuring logging to console. Your configuration is using deprecated syntax for configuring logging to console.
@ -455,6 +501,20 @@ defmodule Pleroma.Config.DeprecationWarnings do
false false
end end
if backend or console, do: :error, else: :ok conflict =
if (!is_nil(use_new_backend_config) and declared_deprecated_backend) or
(declared_new_console and declared_deprecated_console) do
Logger.warning("""
!!!CONFIGURATION CONFLICT!!!
Pleroma has detected that both the deprecated way of configuring Logger and the new way are defined in your configuration.
Delete the deprecated configuration syntax.
""")
true
else
false
end
if backend or console or conflict, do: :error, else: :ok
end end
end end

View file

@ -401,7 +401,8 @@ defmodule Pleroma.Config.DeprecationWarningsTest do
end end
test "warns on deprecated syntax" do test "warns on deprecated syntax" do
log = capture_log(fn -> log =
capture_log(fn ->
Pleroma.Config.DeprecationWarnings.check_deprecated_logger_config() Pleroma.Config.DeprecationWarnings.check_deprecated_logger_config()
end) end)