Elixir 1.19: Replace ugly old logger config checks with config merging
Side note: Going up in log levels doesn't work properly. - debug->info works - info->debug does not work
This commit is contained in:
parent
9450fba83d
commit
0f262bb2f3
3 changed files with 51 additions and 60 deletions
|
|
@ -132,6 +132,9 @@ defmodule Pleroma.Application do
|
|||
{:ok, _} ->
|
||||
Logger.debug("Successfully added logger backend: #{inspect(backend)}")
|
||||
|
||||
{:error, :already_present} ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed to add logger backend #{inspect(backend)}: #{inspect(reason)}")
|
||||
end
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ defmodule Pleroma.Config.DeprecationWarnings do
|
|||
"\n* `config :pleroma, :instance, mrf_transparency_exclusions` is now `config :pleroma, :mrf, transparency_exclusions`"}
|
||||
]
|
||||
|
||||
@logger_config_knobs [:level, :translator_inspect_opts]
|
||||
@logger_formatter_config_knows [:colors, :format, :metadata, :truncate, :utc_log]
|
||||
|
||||
def check_exiftool_filter do
|
||||
filters = Config.get([Pleroma.Upload]) |> Keyword.get(:filters, [])
|
||||
|
||||
|
|
@ -417,54 +420,33 @@ defmodule Pleroma.Config.DeprecationWarnings do
|
|||
end
|
||||
end
|
||||
|
||||
defp merge_deprecated_logger_config(config) do
|
||||
handler_config = Enum.filter(config, fn {k, _} -> k in @logger_config_knobs end)
|
||||
formatter_config = Enum.filter(config, fn {k, _} -> k in @logger_formatter_config_knows end)
|
||||
formatter = Logger.default_formatter(formatter_config)
|
||||
|
||||
Logger.debug("""
|
||||
Reconfiguring console Logger with deprecated configuration syntax.
|
||||
Handler configuration:
|
||||
#{inspect(handler_config)}
|
||||
|
||||
Formatter:
|
||||
#{inspect(formatter)}
|
||||
""")
|
||||
|
||||
Logger.configure(handler_config)
|
||||
:logger.update_handler_config(:default, :formatter, formatter)
|
||||
end
|
||||
|
||||
@spec check_deprecated_logger_config() :: :ok | :error
|
||||
def check_deprecated_logger_config 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])
|
||||
backends_config = Application.get_env(:logger, :backends)
|
||||
console_config = Application.get_env(:logger, :console)
|
||||
|
||||
# Note: No need to merge the old backends config since it still works.
|
||||
# And new configuration will just add new Logger backends.
|
||||
backend =
|
||||
if declared_deprecated_backend do
|
||||
if backends_config do
|
||||
Logger.warning("""
|
||||
!!!DEPRECATION WARNING!!!
|
||||
Your configuration is using deprecated syntax for configuring backends of Elixir's logger.
|
||||
|
|
@ -483,7 +465,7 @@ defmodule Pleroma.Config.DeprecationWarnings do
|
|||
end
|
||||
|
||||
console =
|
||||
if declared_deprecated_console do
|
||||
if console_config do
|
||||
Logger.warning("""
|
||||
!!!DEPRECATION WARNING!!!
|
||||
Your configuration is using deprecated syntax for configuring logging to console.
|
||||
|
|
@ -496,25 +478,13 @@ defmodule Pleroma.Config.DeprecationWarnings do
|
|||
`:default_formatter`. For more info visit: https://hexdocs.pm/logger/Logger.html#module-backends-and-backwards-compatibility
|
||||
""")
|
||||
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
|
||||
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.
|
||||
""")
|
||||
merge_deprecated_logger_config(console_config)
|
||||
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
|
||||
if backend or console or conflict, do: :error, else: :ok
|
||||
if backend or console, do: :error, else: :ok
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -392,11 +392,12 @@ defmodule Pleroma.Config.DeprecationWarningsTest do
|
|||
describe "check_deprecated_logger_config" do
|
||||
setup do
|
||||
Application.put_env(:logger, :backends, [:console, {ExSyslogger, :ex_syslogger}])
|
||||
Application.put_env(:logger, :console, level: :warning)
|
||||
Application.put_env(:logger, :console, level: :debug)
|
||||
|
||||
on_exit(fn ->
|
||||
Application.delete_env(:logger, :backends)
|
||||
Application.delete_env(:logger, :console)
|
||||
:logger.update_handler_config(:default, :formatter, Logger.default_formatter())
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
@ -432,5 +433,22 @@ defmodule Pleroma.Config.DeprecationWarningsTest do
|
|||
`:default_formatter`. For more info visit: https://hexdocs.pm/logger/Logger.html#module-backends-and-backwards-compatibility
|
||||
"""
|
||||
end
|
||||
|
||||
test "reconfigures logger" do
|
||||
log =
|
||||
capture_log(fn ->
|
||||
Pleroma.Config.DeprecationWarnings.check_deprecated_logger_config()
|
||||
end)
|
||||
|
||||
# Formatter has inconsitent color formatting order
|
||||
assert log =~
|
||||
"""
|
||||
Reconfiguring console Logger with deprecated configuration syntax.
|
||||
Handler configuration:
|
||||
[level: :debug]
|
||||
|
||||
Formatter:
|
||||
"""
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue