Skip to content

Emit notice logs in :console backend #11304

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 17 commits into from
Oct 9, 2021
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
4 changes: 2 additions & 2 deletions lib/ex_unit/test/ex_unit/capture_log_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ defmodule ExUnit.CaptureLogTest do

assert logged
assert logged =~ "[info] one\n"
assert logged =~ "[warn] two\n"
assert logged =~ "[warning] two\n"
assert logged =~ "[debug] three\n"
assert logged =~ "[error] one\n"

receive do
{:nested, logged} ->
assert logged =~ "[error] one\n"
refute logged =~ "[warn] two\n"
refute logged =~ "[warning] two\n"
end
end

Expand Down
12 changes: 9 additions & 3 deletions lib/logger/lib/logger/backends/console.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ defmodule Logger.Backends.Console do
def handle_event({level, _gl, {Logger, msg, ts, md}}, state) do
%{level: log_level, ref: ref, buffer_size: buffer_size, max_buffer: max_buffer} = state

{:erl_level, level} = List.keyfind(md, :erl_level, 0, {:erl_level, level})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

md is defined to be keyword list, so why use List.keyfind/4 instead of Keyword.get/3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are fine. List.keyfind can be a tiny bit more efficient.


cond do
not meet_level?(level, log_level) ->
{:ok, state}
Expand Down Expand Up @@ -232,10 +234,14 @@ defmodule Logger.Backends.Console do
colors = Keyword.get(config, :colors, [])

%{
debug: Keyword.get(colors, :debug, :cyan),
info: Keyword.get(colors, :info, :normal),
warn: Keyword.get(colors, :warn, :yellow),
emergency: Keyword.get(colors, :error, :red),
alert: Keyword.get(colors, :error, :red),
critical: Keyword.get(colors, :error, :red),
error: Keyword.get(colors, :error, :red),
warning: Keyword.get(colors, :warn, :yellow),
notice: Keyword.get(colors, :info, :normal),
info: Keyword.get(colors, :info, :normal),
debug: Keyword.get(colors, :debug, :cyan),
enabled: Keyword.get(colors, :enabled, IO.ANSI.enabled?())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order of the keys in this map is now weird. Earlier it was in decreasing verbosity, and now it is "whatever".

end
Expand Down
4 changes: 2 additions & 2 deletions lib/logger/lib/logger/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ defmodule Logger.Formatter do
defp output(:levelpad, level, _, _, _), do: levelpad(level)
defp output(other, _, _, _, _), do: other

defp levelpad(:debug), do: ""
# TODO: Deprecate me on Elixir v1.13+ or later
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

@josevalim josevalim Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

levelpad is not very useful now when one of the levels is "critical". There will be just too much padding on something like "info" (4 chars to be more precise).

defp levelpad(:info), do: " "
defp levelpad(:warn), do: " "
defp levelpad(:error), do: ""
defp levelpad(_), do: ""

defp metadata([{key, value} | metadata]) do
if formatted = metadata(key, value) do
Expand Down
12 changes: 12 additions & 0 deletions lib/logger/test/logger/backends/console_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ defmodule Logger.Backends.ConsoleTest do
assert capture_log(fn -> Logger.debug("hello") end) == ""
end

test "filter by notice" do
Logger.configure_backend(:console, level: :notice)

assert capture_log(fn -> Logger.debug("hello") end) == ""
assert capture_log(fn -> Logger.info("hello") end) == ""
assert capture_log(fn -> Logger.notice("hello") end) =~ "[notice] hello\n"
assert capture_log(fn -> Logger.warning("hello") end) =~ "[warning] hello\n"
assert capture_log(fn -> Logger.critical("hello") end) =~ "[critical] hello\n"
assert capture_log(fn -> Logger.alert("hello") end) =~ "[alert] hello\n"
assert capture_log(fn -> Logger.error("hello") end) =~ "[error] hello\n"
end

test "configures colors" do
Logger.configure_backend(:console, format: "$message", colors: [enabled: true])

Expand Down
13 changes: 7 additions & 6 deletions lib/logger/test/logger/handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Logger.HandlerTest do

assert capture_log(fn ->
:error_logger.info_msg('world: ~p', [:ok])
end) =~ "[info] rewritten"
end) =~ "[notice] rewritten"
after
assert Logger.remove_translator({CustomTranslator, :t})
end
Expand Down Expand Up @@ -97,12 +97,13 @@ defmodule Logger.HandlerTest do
end) =~ "[info] :formatted"
end

test "converts log levels" do
assert capture_log(fn -> :logger.emergency('ok') end) =~ "[error] ok"
assert capture_log(fn -> :logger.alert('ok') end) =~ "[error] ok"
assert capture_log(fn -> :logger.critical('ok') end) =~ "[error] ok"
test "uses Erlang log levels" do
assert capture_log(fn -> :logger.emergency('ok') end) =~ "[emergency] ok"
assert capture_log(fn -> :logger.alert('ok') end) =~ "[alert] ok"
assert capture_log(fn -> :logger.critical('ok') end) =~ "[critical] ok"
assert capture_log(fn -> :logger.error('ok') end) =~ "[error] ok"
assert capture_log(fn -> :logger.warning('ok') end) =~ "[warn] ok"
assert capture_log(fn -> :logger.warning('ok') end) =~ "[warning] ok"
assert capture_log(fn -> :logger.notice('ok') end) =~ "[notice] ok"
assert capture_log(fn -> :logger.info('ok') end) =~ "[info] ok"
assert capture_log(fn -> :logger.debug('ok') end) =~ "[debug] ok"
end
Expand Down
16 changes: 8 additions & 8 deletions lib/logger/test/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,15 @@ defmodule LoggerTest do
test "deprecated :warn" do
assert capture_log(fn ->
Logger.warn("hello") == :ok
end) =~ "[warn]"
end) =~ "[warning]"

assert capture_log(fn ->
Logger.log(:warn, "hello") == :ok
end) =~ "[warn]"
end) =~ "[warning]"

assert capture_log(fn ->
Logger.bare_log(:warn, "hello") == :ok
end) =~ "[warn]"
end) =~ "[warning]"
end

describe "levels" do
Expand Down Expand Up @@ -451,7 +451,7 @@ defmodule LoggerTest do
test "warning/2" do
assert capture_log(fn ->
assert Logger.warning("hello", []) == :ok
end) =~ msg_with_meta("[warn] hello")
end) =~ msg_with_meta("[warning] hello")

assert capture_log(:error, fn ->
assert Logger.warning("hello", []) == :ok
Expand All @@ -465,7 +465,7 @@ defmodule LoggerTest do
test "warn/2" do
assert capture_log(fn ->
assert Logger.warn("hello", []) == :ok
end) =~ msg_with_meta("[warn] hello")
end) =~ msg_with_meta("[warning] hello")

assert capture_log(:error, fn ->
assert Logger.warn("hello", []) == :ok
Expand Down Expand Up @@ -493,7 +493,7 @@ defmodule LoggerTest do
test "critical/2" do
assert capture_log(fn ->
assert Logger.critical("hello", []) == :ok
end) =~ msg_with_meta("[error] hello")
end) =~ msg_with_meta("[critical] hello")

assert capture_log(:alert, fn ->
assert Logger.critical("hello", []) == :ok
Expand All @@ -507,7 +507,7 @@ defmodule LoggerTest do
test "alert/2" do
assert capture_log(fn ->
assert Logger.alert("hello", []) == :ok
end) =~ msg_with_meta("[error] hello")
end) =~ msg_with_meta("[alert] hello")

assert capture_log(:emergency, fn ->
assert Logger.alert("hello", []) == :ok
Expand All @@ -521,7 +521,7 @@ defmodule LoggerTest do
test "emergency/2" do
assert capture_log(fn ->
assert Logger.emergency("hello", []) == :ok
end) =~ msg_with_meta("[error] hello")
end) =~ msg_with_meta("[emergency] hello")

assert capture_log(:none, fn ->
assert Logger.emergency("hello", []) == :ok
Expand Down