Skip to content

Commit a592661

Browse files
author
José Valim
committed
Ensure log configuration triggers a restart
1 parent 59910ae commit a592661

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

lib/logger/lib/logger.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ defmodule Logger do
144144
The supported keys in the `:colors` keyword list are:
145145
146146
* `:enabled` - boolean value that allows for switching the
147-
coloring on and off. Defaults to: `false`
147+
coloring on and off. Defaults to: `false`
148148
149149
* `:debug` - color for debug messages. Defaults to: `:magenta`
150150

lib/logger/lib/logger/backends/console.ex

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ defmodule Logger.Backends.Console do
4545
end
4646

4747
defp configure_merge(env, options) do
48-
env_colors = Keyword.get(env, :colors, [])
49-
opts_colors = Keyword.get(options, :colors, [])
50-
colors = Keyword.merge(env_colors, opts_colors)
51-
52-
Keyword.merge(env, options)
53-
|> Keyword.put(:colors, colors)
48+
Keyword.merge(env, options, fn
49+
:colors, v1, v2 -> Keyword.merge(v1, v2)
50+
_, _v1, v2 -> v2
51+
end)
5452
end
5553

5654
defp configure_colors(console) do
@@ -73,8 +71,8 @@ defmodule Logger.Backends.Console do
7371
Logger.Formatter.format(format, level, msg, ts, Dict.take(md, metadata))
7472
end
7573

76-
defp color_event(level, data, %{enabled: enabled} = colors) do
77-
IO.ANSI.format([Map.fetch!(colors, level) | data], enabled)
78-
end
79-
74+
defp color_event(level, data, %{enabled: true} = colors), do:
75+
[IO.ANSI.format_fragment(Map.fetch!(colors, level), true), data|IO.ANSI.reset]
76+
defp color_event(_level, data, %{enabled: false}), do:
77+
data
8078
end

lib/logger/test/logger/backends/console_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Logger.Backends.ConsoleTest do
55
setup do
66
on_exit fn ->
77
:ok = Logger.configure_backend(:console,
8-
[format: nil, level: nil, metadata: [], colors: [enabled: false]])
8+
[format: nil, level: nil, metadata: [], colors: [enabled: false]])
99
end
1010
end
1111

lib/mix/lib/mix/tasks/loadconfig.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ defmodule Mix.Tasks.Loadconfig do
2727
end
2828

2929
defp load(file) do
30-
Mix.Config.persist Mix.Config.read! file
30+
opts = Mix.Config.read!(file)
31+
Mix.Config.persist(opts)
32+
33+
# In case the Logger was changed, we reload the whole
34+
# Logger. This is required because many of Logger config
35+
# reflects on its supervision tree. No other application
36+
# that is bundled with Elixir requires such reloading.
37+
if opts[:logger], do: Logger.Config.restart
38+
39+
:ok
3140
end
3241
end

lib/mix/test/mix/tasks/loadconfig_test.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ defmodule Mix.Tasks.LoadconfigTest do
2121

2222
in_fixture "no_mixfile", fn ->
2323
write_config """
24-
[my_app: [key: :project]]
24+
[my_app: [key: :project],
25+
logger: [level: :error]]
2526
"""
2627

28+
# Original Logger level
29+
assert Logger.level == :debug
30+
2731
assert Application.fetch_env(:my_app, :key) == :error
2832
Mix.Task.run "loadconfig", []
2933
assert Application.fetch_env(:my_app, :key) == {:ok, :project}
@@ -32,11 +36,16 @@ defmodule Mix.Tasks.LoadconfigTest do
3236
:ok = :application.load({:application, :my_app, [vsn: '1.0.0', env: [key: :app]]})
3337
assert Application.fetch_env(:my_app, :key) == {:ok, :project}
3438

39+
# Logger is reloaded
40+
assert Logger.level == :error
41+
3542
# laodconfig can be called multiple times
3643
# Later values should have higher precedence
3744
Mix.Task.run "loadconfig", [fixture_path("configs/good_config.exs")]
3845
assert Application.fetch_env(:my_app, :key) == {:ok, :value}
3946
end
47+
after
48+
Logger.configure(level: :debug)
4049
end
4150

4251
defp write_config(path \\ "config/config.exs", contents) do

0 commit comments

Comments
 (0)