Skip to content

Make Logger.Formatter an official formatter for Erlang logger #12400

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

Merged
merged 18 commits into from
Feb 15, 2023
Merged
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
5 changes: 2 additions & 3 deletions lib/elixir/lib/kernel/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ defmodule Kernel.CLI do
{_, status} = at_exit({ok_or_shutdown, status})

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

System.halt(status)
Expand Down
37 changes: 20 additions & 17 deletions lib/ex_unit/lib/ex_unit/capture_log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule ExUnit.CaptureLog do

alias Logger.Backends.Console

@compile {:no_warn_undefined, Logger}
@compile {:no_warn_undefined, [Logger, Logger.Backends.Internal]}

@doc """
Captures Logger messages generated when evaluating `fun`.
Expand Down Expand Up @@ -138,24 +138,27 @@ defmodule ExUnit.CaptureLog do

@doc false
def init_proxy(pid, opts, parent) do
case :gen_event.add_sup_handler(Logger, {Console, pid}, {Console, [device: pid] ++ opts}) do
:ok ->
ref = Process.monitor(parent)
:proc_lib.init_ack(:ok)

receive do
{:DOWN, ^ref, :process, ^parent, _reason} -> :ok
{:gen_event_EXIT, {Console, ^pid}, _reason} -> :ok
end
Logger.Backends.Internal.configure([])
catch
_, _ -> :proc_lib.init_ack(:noproc)
else
_ ->
case :gen_event.add_sup_handler(Logger, {Console, pid}, {Console, [device: pid] ++ opts}) do
:ok ->
ref = Process.monitor(parent)
:proc_lib.init_ack(:ok)

receive do
{:DOWN, ^ref, :process, ^parent, _reason} -> :ok
{:gen_event_EXIT, {Console, ^pid}, _reason} -> :ok
end

{:EXIT, reason} ->
:proc_lib.init_ack({:error, reason})
{:EXIT, reason} ->
:proc_lib.init_ack({:error, reason})

{:error, reason} ->
:proc_lib.init_ack({:error, reason})
end
catch
:exit, :noproc -> :proc_lib.init_ack(:noproc)
{:error, reason} ->
:proc_lib.init_ack({:error, reason})
end
end

defp remove_capture(pid) do
Expand Down
21 changes: 15 additions & 6 deletions lib/ex_unit/lib/ex_unit/capture_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule ExUnit.CaptureServer do
state = %{
devices: %{},
log_captures: %{},
log_status: nil
log_status: :error
}

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

if map_size(refs) == 1 do
status = Logger.remove_backend(:console)
status =
with {:ok, config} <- :logger.get_handler_config(:default),
:ok <- :logger.remove_handler(:default) do
{:ok, config}
else
_ -> :error
end

{:reply, ref, %{config | log_captures: refs, log_status: status}}
else
{:reply, ref, %{config | log_captures: refs}}
Expand Down Expand Up @@ -189,9 +196,11 @@ defmodule ExUnit.CaptureServer do
end
end

defp maybe_add_console(refs, status) do
if status == :ok and map_size(refs) == 0 do
Logger.add_backend(:console, flush: true)
end
defp maybe_add_console(refs, {:ok, %{module: module} = config}) when map_size(refs) == 0 do
:logger.add_handler(:default, module, config)
end

defp maybe_add_console(_refs, _config) do
:ok
end
end
8 changes: 1 addition & 7 deletions lib/ex_unit/test/ex_unit/capture_log_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ defmodule ExUnit.CaptureLogTest do
use ExUnit.Case

require Logger

import ExUnit.CaptureLog

setup_all do
:ok = Logger.remove_backend(:console)
on_exit(fn -> Logger.add_backend(:console, flush: true) end)
end

test "no output" do
assert capture_log(fn -> nil end) == ""
end
Expand Down Expand Up @@ -96,7 +90,7 @@ defmodule ExUnit.CaptureLogTest do

defp wait_capture_removal() do
case :gen_event.which_handlers(Logger) do
[Logger.Config] ->
[Logger.Backends.Config] ->
:ok

_otherwise ->
Expand Down
14 changes: 1 addition & 13 deletions lib/ex_unit/test/ex_unit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,8 @@ defmodule ExUnitTest do
test "log capturing" do
defmodule LogCapturingTest do
use ExUnit.Case

require Logger

setup_all do
:ok = Logger.remove_backend(:console)
on_exit(fn -> Logger.add_backend(:console, flush: true) end)
end

@tag :capture_log
test "one" do
Logger.debug("one")
Expand All @@ -341,18 +335,12 @@ defmodule ExUnitTest do
Logger.debug("three")
assert 1 == 2
end

test "four" do
Logger.debug("four")
assert 1 == 2
end
end

output = capture_io(&ExUnit.run/0)
assert output =~ "[debug] two\n"
refute output =~ "[debug] one\n"
assert output =~ "[debug] two\n"
assert output =~ "[debug] three\n"
refute output =~ "[debug] four\n"
end

test "supports multi errors" do
Expand Down
Loading