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

Make logger_translator not be a primary filter #13741

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/logger/lib/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,16 @@ defmodule Logger do
translators = updater.(Application.fetch_env!(:logger, :translators))
Application.put_env(:logger, :translators, translators)

with %{filters: filters} <- :logger.get_primary_config(),
with {:ok, %{filters: filters}} <- :logger.get_handler_config(:default),
{{_, {fun, config}}, filters} <- List.keytake(filters, :logger_translator, 0) do
config = %{config | translators: translators}
:ok = :logger.set_primary_config(:filters, filters ++ [logger_translator: {fun, config}])

:ok =
:logger.set_handler_config(
:default,
:filters,
[logger_translator: {fun, config}] ++ filters
)
end
end)

Expand Down
16 changes: 8 additions & 8 deletions lib/logger/lib/logger/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Logger.App do
otp_reports? = Application.fetch_env!(:logger, :handle_otp_reports)
sasl_reports? = Application.fetch_env!(:logger, :handle_sasl_reports)
translators = Application.fetch_env!(:logger, :translators)
translator_config = %{translators: translators, otp: otp_reports?, sasl: sasl_reports?}

{backends, console?} =
case Application.fetch_env(:logger, :backends) do
Expand Down Expand Up @@ -43,16 +44,12 @@ defmodule Logger.App do
process_level_filter = {&Logger.Utils.process_level/2, []}
:ok = :logger.add_primary_filter(:logger_process_level, process_level_filter)

translator_config = %{translators: translators, otp: otp_reports?, sasl: sasl_reports?}
translator_filter = {&Logger.Utils.translator/2, translator_config}
:ok = :logger.add_primary_filter(:logger_translator, translator_filter)

revert = [{:set_primary_config, [primary_config]} | remove_erlang_handler()]
children = if backends != [], do: [Logger.Backends.Internal], else: []

case Supervisor.start_link(children, strategy: :one_for_one, name: Logger.Supervisor) do
{:ok, sup} ->
{:ok, sup, [add_elixir_handler(console? && default_handler) | revert]}
{:ok, sup, [add_elixir_handler(console? && default_handler, translator_config) | revert]}

{:error, _} = error ->
redo(revert)
Expand All @@ -70,7 +67,7 @@ defmodule Logger.App do
# TODO: Remove this line and all of Logger.Backends.* on Elixir v2.0+
_ = :logger.remove_handler(Logger)
_ = :logger.remove_primary_filter(:logger_process_level)
_ = :logger.remove_primary_filter(:logger_translator)
# _ = :logger.remove_primary_filter(:logger_translator)

redo(revert)

Expand Down Expand Up @@ -119,11 +116,14 @@ defmodule Logger.App do
end
end

defp add_elixir_handler(default_handler) do
defp add_elixir_handler(default_handler, translator_config) do
if handler = Application.get_env(:logger, :default_handler, default_handler) do
config =
handler
|> Keyword.put_new(:filters, remote_gl: {&:logger_filters.remote_gl/2, :stop})
|> Keyword.put_new(:filters,
logger_translator: {fn a, b -> Logger.Utils.translator(a, b) end, translator_config},
remote_gl: {&:logger_filters.remote_gl/2, :stop}
)
|> Keyword.put_new(:filter_default, :log)
|> Keyword.put_new(:module, :logger_std_h)
|> Keyword.put_new_lazy(:formatter, &Logger.default_formatter/0)
Expand Down
3 changes: 3 additions & 0 deletions lib/logger/test/logger/translator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ defmodule Logger.TranslatorTest do
:ignore
end

{:ok, %{filters: [{key, filter} | _]}} = :logger.get_handler_config(:default)
:logger.remove_handler_filter(:default, key)
:logger.add_handler_filter(:default, :forwarder, {fun, self()})
:logger.add_handler_filter(:default, key, filter)
on_exit(fn -> :logger.remove_handler_filter(:default, :forwarder) end)
end

Expand Down