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

Give prevalence to process level over primary/module levels #13955

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 20 additions & 5 deletions lib/logger/lib/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -928,19 +928,34 @@ defmodule Logger do
def bare_log(level, message_or_fun, metadata \\ []) do
level = elixir_level_to_erlang_level(level)

if :logger_config.allow(level) do
should_log? =
case get_process_level(self()) do
nil -> :logger_config.allow(level)
process_level -> compare_levels(level, process_level) in [:gt, :eq]
end

if should_log? do
__do_log__(level, message_or_fun, %{}, Map.new(metadata))
else
:ok
end

:ok
end

# If there's a process level, it takes precedence over global/module levels.
@doc false
def __should_log__(level, module) do
level = elixir_level_to_erlang_level(level)

if :logger_config.allow(level, module) do
level
case get_process_level(self()) do
v0idpwn marked this conversation as resolved.
Show resolved Hide resolved
nil ->
if :logger_config.allow(level, module) do
level
end

process_level ->
Copy link
Member

Choose a reason for hiding this comment

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

We still need to check if this level is allowed by either calling :logger_config.allow(level) or by calling :logger_config.allow(level, module) if we want still want to look up the module.

Copy link
Contributor Author

@v0idpwn v0idpwn Nov 3, 2024

Choose a reason for hiding this comment

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

Calling :logger_config.allow(level, module) there would give precedence for module/primary level. Pushed some fixes and I believe we have the expected behaviour now :)

if compare_levels(level, process_level) in [:gt, :eq] do
level
end
end
end

Expand Down
38 changes: 31 additions & 7 deletions lib/logger/test/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ defmodule LoggerTest do
end)
end

defmodule PerModuleLevels do
def debug, do: Logger.debug("debug_msg")
def error, do: Logger.error("error_msg")
end

defp msg_with_meta(text) do
msg("module=LoggerTest #{text}")
end
Expand Down Expand Up @@ -55,12 +60,6 @@ defmodule LoggerTest do
end

test "per-module levels" do
defmodule PerModuleLevels do
def debug, do: Logger.debug("debug_msg")

def error, do: Logger.error("error_msg")
end

assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) =~ "debug_msg"
assert capture_log(fn -> assert PerModuleLevels.error() == :ok end) =~ "error_msg"

Expand Down Expand Up @@ -181,6 +180,31 @@ defmodule LoggerTest do
assert capture_log(fn -> Logger.error("error_msg") end) =~ "error_msg"
end

test "process levels override primary level" do
current_level = Logger.level()
on_exit(fn -> Logger.configure(level: current_level) end)
Logger.configure(level: :error)

Logger.put_process_level(self(), :debug)
assert capture_log(fn -> Logger.debug("debug_msg") end) =~ "debug_msg"

Logger.put_process_level(self(), :emergency)
assert capture_log(fn -> Logger.error("error_msg") end) == ""

assert capture_log(fn -> Logger.emergency("emergency_msg") end) =~ "emergency_msg"

Logger.delete_process_level(self())
assert ExUnit.CaptureLog.capture_log(fn -> Logger.debug("debug_msg") end) == ""
end

test "process levels override module level" do
Logger.put_module_level(PerModuleLevels, :error)
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) =~ ""

Logger.put_process_level(self(), :debug)
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) =~ "debug_msg"
end

test "process metadata" do
assert Logger.metadata(data: true) == :ok
assert Logger.metadata() == [data: true]
Expand Down Expand Up @@ -760,7 +784,7 @@ defmodule LoggerTest do

test "maps Erlang levels" do
:logger.set_primary_config(:level, :notice)
assert capture_log(fn -> Logger.info("hello") end) =~ "hello"
assert capture_log(fn -> Logger.warning("hello") end) =~ "hello"
Copy link
Contributor Author

@v0idpwn v0idpwn Nov 3, 2024

Choose a reason for hiding this comment

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

I'm not sure what's the behaviour that was being tested here. This started failing when I made capture_log not change log level


:logger.set_primary_config(:level, :notice)
assert Logger.level() == :notice
Expand Down
17 changes: 10 additions & 7 deletions lib/logger/test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ defmodule Logger.Case do
end
end

def capture_log(level \\ :debug, fun) do
def capture_log(level \\ Logger.level(), fun) do
previous_level = Logger.level()
Logger.configure(level: level)

capture_io(:user, fn ->
fun.()
Logger.flush()
end)
after
Logger.configure(level: :debug)
try do
capture_io(:user, fn ->
fun.()
Logger.flush()
end)
after
Logger.configure(level: previous_level)
end
end
end