From 5e490b61a31d2fd5f0f797a9e6c1076d00683917 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Sun, 3 Nov 2024 11:50:22 -0300 Subject: [PATCH 1/2] Give prevalence to process level over global level --- lib/logger/lib/logger.ex | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/logger/lib/logger.ex b/lib/logger/lib/logger.ex index 8a9bdb20bb..2aa685e0a4 100644 --- a/lib/logger/lib/logger.ex +++ b/lib/logger/lib/logger.ex @@ -939,8 +939,14 @@ defmodule Logger do 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 + nil -> + if :logger_config.allow(level, module) do + level + end + + process_level -> + process_level end end From 0a15c7331596f40584930dde43d01c63589cdaed Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Sun, 3 Nov 2024 15:50:34 -0300 Subject: [PATCH 2/2] Make it actually work --- lib/logger/lib/logger.ex | 17 +++++++++++---- lib/logger/test/logger_test.exs | 38 +++++++++++++++++++++++++++------ lib/logger/test/test_helper.exs | 17 +++++++++------ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/lib/logger/lib/logger.ex b/lib/logger/lib/logger.ex index 2aa685e0a4..3df45945fa 100644 --- a/lib/logger/lib/logger.ex +++ b/lib/logger/lib/logger.ex @@ -928,13 +928,20 @@ 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) @@ -946,7 +953,9 @@ defmodule Logger do end process_level -> - process_level + if compare_levels(level, process_level) in [:gt, :eq] do + level + end end end diff --git a/lib/logger/test/logger_test.exs b/lib/logger/test/logger_test.exs index 2d26722711..887ef093d3 100644 --- a/lib/logger/test/logger_test.exs +++ b/lib/logger/test/logger_test.exs @@ -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 @@ -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" @@ -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] @@ -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" :logger.set_primary_config(:level, :notice) assert Logger.level() == :notice diff --git a/lib/logger/test/test_helper.exs b/lib/logger/test/test_helper.exs index fa35525c25..f5d65c1e10 100644 --- a/lib/logger/test/test_helper.exs +++ b/lib/logger/test/test_helper.exs @@ -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