diff --git a/lib/logger/lib/logger.ex b/lib/logger/lib/logger.ex index 8a9bdb20bb..3df45945fa 100644 --- a/lib/logger/lib/logger.ex +++ b/lib/logger/lib/logger.ex @@ -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 + nil -> + if :logger_config.allow(level, module) do + level + end + + 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