Skip to content

Commit

Permalink
Use erlang log level if none is set
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Nov 1, 2019
1 parent 75048ba commit 5e6c5dd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
1 change: 0 additions & 1 deletion lib/logger/lib/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,6 @@ defmodule Logger do
Logger.Config.configure(options)

# Then we can read from the writes
Logger.Config.load_log_level()
:ok = :logger.set_handler_config(Logger, %{config: %{}})
end

Expand Down
35 changes: 27 additions & 8 deletions lib/logger/lib/logger/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ defmodule Logger.App do
end

@doc false
def config_change(_changed, _new, _removed) do
Logger.configure([])
def config_change(changed, _new, _removed) do
Logger.configure(changed)
end

@doc """
Expand All @@ -81,15 +81,34 @@ defmodule Logger.App do
end
}

%{level: level} = primary_config = :logger.get_primary_config()
%{level: erl_level} = primary_config = :logger.get_primary_config()

# Elixir's logger level is no longer set by default.
#
# If it is set, it always has higher precedence, but we warn
# in case of mismatches.
#
# If it is not set, we revert Erlang's kernel to debug, if it
# has its default value, otherwise we keep it as is.
case Application.fetch_env(:logger, :level) do
{:ok, level} ->
if erl_level != :notice and erl_level != level do
IO.warn(
"the level for Erlang's logger was set to #{inspect(erl_level)}, " <>
"but Elixir's logger was set to #{inspect(level)}. " <>
"Elixir's logger value will take higher precedence"
)
end

:ok = :logger.set_primary_config(:level, level)

:error when erl_level == :notice ->
:ok = :logger.set_primary_config(:level, :debug)

if level != :notice and Application.fetch_env!(:logger, :level) != level do
IO.warn "the level for Erlang's logger was set to #{inspect(level)}, " <>
"but Elixir's logger was set to #{inspect(Application.fetch_env!(:logger, :level))}. " <>
"Elixir's logger value will take higher precedence"
:error ->
:ok
end

Logger.Config.load_log_level()
:ok = :logger.add_primary_filter(:process_disabled, {&Logger.Filter.process_disabled/2, []})
:ok = :logger.add_handler(Logger, Logger.Handler, config)
primary_config
Expand Down
22 changes: 10 additions & 12 deletions lib/logger/lib/logger/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ defmodule Logger.Config do

alias Logger.Counter

def load_log_level do
level =
case Application.fetch_env!(:logger, :level) do
:warn -> :warning
level -> level
end

:ok = :logger.set_primary_config(:level, level)
end

def configure(options) do
:gen_event.call(Logger, @name, {:configure, options})
end
Expand Down Expand Up @@ -55,8 +45,16 @@ defmodule Logger.Config do
end

def handle_call({:configure, options}, {counter, _, _, _}) do
Enum.each(options, fn {key, value} ->
Application.put_env(:logger, key, value)
Enum.each(options, fn
# TODO: Warn on deprecated level
{:level, :warn} ->
:ok = :logger.set_primary_config(:level, :warning)

{:level, level} ->
:ok = :logger.set_primary_config(:level, level)

{key, value} ->
Application.put_env(:logger, key, value)
end)

{:ok, :ok, load_state(counter)}
Expand Down
5 changes: 3 additions & 2 deletions lib/logger/lib/logger/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ defmodule Logger.Handler do
metadata = erlang_metadata_to_elixir_metadata(metadata)
%{truncate: truncate, utc_log: utc_log?} = config

# TODO: Use `time` field of `erl_metadata` for timestamp
event = {
level,
gl,
Expand Down Expand Up @@ -165,7 +164,9 @@ defmodule Logger.Handler do

## Metadata helpers

# TODO: We should only do this for legacy handlers
# TODO: We should only do this for legacy handlers.
# The new handlers should accept all metadata as is
# and receive the system time unit rather than tuples.
defp erlang_metadata_to_elixir_metadata(metadata) do
metadata =
case metadata do
Expand Down
1 change: 0 additions & 1 deletion lib/logger/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Logger.MixProject do
registered: [Logger, Logger.BackendSupervisor, Logger.Supervisor, Logger.Watcher],
mod: {Logger.App, []},
env: [
level: :debug,
utc_log: false,
truncate: 8096,
backends: [:console],
Expand Down
1 change: 0 additions & 1 deletion lib/logger/test/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ defmodule LoggerTest do
:ok = Logger.configure(level: :debug)

try do
Application.put_env(:logger, :level, :error)
assert Logger.App.config_change([level: :error], [], []) === :ok
assert Logger.level() === :error
after
Expand Down

0 comments on commit 5e6c5dd

Please sign in to comment.