Skip to content

Commit edd2944

Browse files
authored
Drop otp log events if handle_otp_reports is set to false (#14354)
1 parent 0fed300 commit edd2944

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

lib/logger/lib/logger/utils.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Logger.Utils do
88
@doc """
99
A filter for default translation and handling of reports.
1010
"""
11-
def translator(%{domain: [:elixir | _]}, %{otp: false}), do: :stop
11+
def translator(%{meta: %{domain: [:otp | _]}}, %{otp: false}), do: :stop
1212
def translator(%{meta: %{domain: [:otp, :sasl | _]}}, %{sasl: false}), do: :stop
1313
def translator(%{meta: %{domain: [:supervisor_report | _]}}, %{sasl: false}), do: :stop
1414
def translator(%{msg: {:string, _}}, _config), do: :ignore

lib/logger/test/logger/translator_test.exs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,27 @@ defmodule Logger.TranslatorTest do
140140
end
141141

142142
setup_all do
143-
sasl_reports? = Application.get_env(:logger, :handle_sasl_reports, false)
144-
Application.put_env(:logger, :handle_sasl_reports, true)
143+
config = Application.get_all_env(:logger)
145144

146-
# Shutdown the application
147-
Logger.App.stop()
145+
config
146+
|> Keyword.put(:handle_sasl_reports, true)
147+
|> put_logger_config()
148148

149-
# And start it without warnings
150-
Application.put_env(:logger, :level, :error)
151-
Application.start(:logger)
152-
Application.delete_env(:logger, :level)
153-
Logger.configure(level: :debug)
149+
on_exit(fn -> restore_logger_config(config) end)
150+
end
154151

155-
on_exit(fn ->
156-
Application.put_env(:logger, :handle_sasl_reports, sasl_reports?)
157-
Logger.App.stop()
158-
Application.start(:logger)
159-
end)
152+
setup context do
153+
test_overrides = Map.get(context, :logger_config, [])
154+
existing_config = Application.get_all_env(:logger)
155+
156+
case Keyword.merge(existing_config, test_overrides) do
157+
^existing_config ->
158+
:ok
159+
160+
new_config ->
161+
put_logger_config(new_config)
162+
on_exit(fn -> restore_logger_config(existing_config) end)
163+
end
160164
end
161165

162166
setup do
@@ -1259,6 +1263,13 @@ defmodule Logger.TranslatorTest do
12591263
:code.delete(WeirdFunctionNamesGenServer)
12601264
end
12611265

1266+
@tag logger_config: [handle_otp_reports: false]
1267+
test "drops events if otp report handling is switched off" do
1268+
{:ok, pid} = GenServer.start(MyGenServer, :ok)
1269+
catch_exit(GenServer.call(pid, :error))
1270+
refute_receive {:event, _, _}, 200
1271+
end
1272+
12621273
def task(parent, fun \\ fn -> raise "oops" end) do
12631274
mon = Process.monitor(parent)
12641275
Process.unlink(parent)
@@ -1312,4 +1323,25 @@ defmodule Logger.TranslatorTest do
13121323
defp worker(name, args, opts \\ []) do
13131324
Enum.into(opts, %{id: name, start: {name, opts[:function] || :start_link, args}})
13141325
end
1326+
1327+
defp put_logger_config(new_config) do
1328+
Application.put_all_env(logger: new_config)
1329+
1330+
Logger.App.stop()
1331+
1332+
# Shutdown the application
1333+
Logger.App.stop()
1334+
1335+
# And start it without warnings
1336+
Application.put_env(:logger, :level, :error)
1337+
Application.start(:logger)
1338+
Application.delete_env(:logger, :level)
1339+
Logger.configure(level: :debug)
1340+
end
1341+
1342+
defp restore_logger_config(config) do
1343+
Application.put_all_env(logger: config)
1344+
Logger.App.stop()
1345+
Application.start(:logger)
1346+
end
13151347
end

0 commit comments

Comments
 (0)