Skip to content

Commit afa44f6

Browse files
authored
Introduce default_formatter and default_handler config for Logger (#12400)
We now fully integrate with Erlang/OTP logger. * Logger.Formatter can now instantiate a compatible Erlamg/OTP logger formatter * The default_handler configures the default handler * The default_formatter configures the default formatter * Backends front-end will be moved to logger_backends package, but the implementation will still be shipped as part of Elixir
1 parent a02e2fa commit afa44f6

28 files changed

+1698
-1330
lines changed

lib/elixir/lib/kernel/cli.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ defmodule Kernel.CLI do
5757
{_, status} = at_exit({ok_or_shutdown, status})
5858

5959
# Ensure Logger messages are flushed before halting
60-
case :erlang.whereis(Logger) do
61-
pid when is_pid(pid) -> Logger.flush()
62-
_ -> :ok
60+
if Code.loaded?(Logger) do
61+
Logger.flush()
6362
end
6463

6564
System.halt(status)

lib/ex_unit/lib/ex_unit/capture_log.ex

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule ExUnit.CaptureLog do
3939

4040
alias Logger.Backends.Console
4141

42-
@compile {:no_warn_undefined, Logger}
42+
@compile {:no_warn_undefined, [Logger, Logger.Backends.Internal]}
4343

4444
@doc """
4545
Captures Logger messages generated when evaluating `fun`.
@@ -138,24 +138,27 @@ defmodule ExUnit.CaptureLog do
138138

139139
@doc false
140140
def init_proxy(pid, opts, parent) do
141-
case :gen_event.add_sup_handler(Logger, {Console, pid}, {Console, [device: pid] ++ opts}) do
142-
:ok ->
143-
ref = Process.monitor(parent)
144-
:proc_lib.init_ack(:ok)
145-
146-
receive do
147-
{:DOWN, ^ref, :process, ^parent, _reason} -> :ok
148-
{:gen_event_EXIT, {Console, ^pid}, _reason} -> :ok
149-
end
141+
Logger.Backends.Internal.configure([])
142+
catch
143+
_, _ -> :proc_lib.init_ack(:noproc)
144+
else
145+
_ ->
146+
case :gen_event.add_sup_handler(Logger, {Console, pid}, {Console, [device: pid] ++ opts}) do
147+
:ok ->
148+
ref = Process.monitor(parent)
149+
:proc_lib.init_ack(:ok)
150+
151+
receive do
152+
{:DOWN, ^ref, :process, ^parent, _reason} -> :ok
153+
{:gen_event_EXIT, {Console, ^pid}, _reason} -> :ok
154+
end
150155

151-
{:EXIT, reason} ->
152-
:proc_lib.init_ack({:error, reason})
156+
{:EXIT, reason} ->
157+
:proc_lib.init_ack({:error, reason})
153158

154-
{:error, reason} ->
155-
:proc_lib.init_ack({:error, reason})
156-
end
157-
catch
158-
:exit, :noproc -> :proc_lib.init_ack(:noproc)
159+
{:error, reason} ->
160+
:proc_lib.init_ack({:error, reason})
161+
end
159162
end
160163

161164
defp remove_capture(pid) do

lib/ex_unit/lib/ex_unit/capture_server.ex

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule ExUnit.CaptureServer do
3636
state = %{
3737
devices: %{},
3838
log_captures: %{},
39-
log_status: nil
39+
log_status: :error
4040
}
4141

4242
{:ok, state}
@@ -64,7 +64,14 @@ defmodule ExUnit.CaptureServer do
6464
refs = Map.put(config.log_captures, ref, true)
6565

6666
if map_size(refs) == 1 do
67-
status = Logger.remove_backend(:console)
67+
status =
68+
with {:ok, config} <- :logger.get_handler_config(:default),
69+
:ok <- :logger.remove_handler(:default) do
70+
{:ok, config}
71+
else
72+
_ -> :error
73+
end
74+
6875
{:reply, ref, %{config | log_captures: refs, log_status: status}}
6976
else
7077
{:reply, ref, %{config | log_captures: refs}}
@@ -189,9 +196,11 @@ defmodule ExUnit.CaptureServer do
189196
end
190197
end
191198

192-
defp maybe_add_console(refs, status) do
193-
if status == :ok and map_size(refs) == 0 do
194-
Logger.add_backend(:console, flush: true)
195-
end
199+
defp maybe_add_console(refs, {:ok, %{module: module} = config}) when map_size(refs) == 0 do
200+
:logger.add_handler(:default, module, config)
201+
end
202+
203+
defp maybe_add_console(_refs, _config) do
204+
:ok
196205
end
197206
end

lib/ex_unit/test/ex_unit/capture_log_test.exs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@ defmodule ExUnit.CaptureLogTest do
44
use ExUnit.Case
55

66
require Logger
7-
87
import ExUnit.CaptureLog
98

10-
setup_all do
11-
:ok = Logger.remove_backend(:console)
12-
on_exit(fn -> Logger.add_backend(:console, flush: true) end)
13-
end
14-
159
test "no output" do
1610
assert capture_log(fn -> nil end) == ""
1711
end
@@ -96,7 +90,7 @@ defmodule ExUnit.CaptureLogTest do
9690

9791
defp wait_capture_removal() do
9892
case :gen_event.which_handlers(Logger) do
99-
[Logger.Config] ->
93+
[Logger.Backends.Config] ->
10094
:ok
10195

10296
_otherwise ->

lib/ex_unit/test/ex_unit_test.exs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,8 @@ defmodule ExUnitTest do
316316
test "log capturing" do
317317
defmodule LogCapturingTest do
318318
use ExUnit.Case
319-
320319
require Logger
321320

322-
setup_all do
323-
:ok = Logger.remove_backend(:console)
324-
on_exit(fn -> Logger.add_backend(:console, flush: true) end)
325-
end
326-
327321
@tag :capture_log
328322
test "one" do
329323
Logger.debug("one")
@@ -341,18 +335,12 @@ defmodule ExUnitTest do
341335
Logger.debug("three")
342336
assert 1 == 2
343337
end
344-
345-
test "four" do
346-
Logger.debug("four")
347-
assert 1 == 2
348-
end
349338
end
350339

351340
output = capture_io(&ExUnit.run/0)
352-
assert output =~ "[debug] two\n"
353341
refute output =~ "[debug] one\n"
342+
assert output =~ "[debug] two\n"
354343
assert output =~ "[debug] three\n"
355-
refute output =~ "[debug] four\n"
356344
end
357345

358346
test "supports multi errors" do

0 commit comments

Comments
 (0)