Skip to content

Commit 4726e0a

Browse files
author
José Valim
committed
Persist calls to add_backend and remove_backend
1 parent ee82679 commit 4726e0a

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lib/logger/lib/logger.ex

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ defmodule Logger do
123123
is explored with detail below.
124124
125125
The initial backends are loaded via the `:backends` configuration,
126-
which must be set before the logger application is started. However,
127-
backends can be added or removed dynamically via the `add_backend/2`,
128-
`remove_backend/1` and `configure_backend/2` functions, although such
129-
backends are not persisted (i.e. if the Logger supervision tree crashes,
130-
they are not re-added).
126+
which must be set before the logger application is started.
131127
132128
### Console backend
133129
@@ -369,13 +365,27 @@ defmodule Logger do
369365
"""
370366
def add_backend(backend, opts \\ []) do
371367
_ = if opts[:flush], do: GenEvent.which_handlers(:error_logger)
372-
Logger.Watcher.watch(Logger, translate_backend(backend), backend)
368+
case Logger.Watcher.watch(Logger, translate_backend(backend), backend) do
369+
{:ok, _} = ok ->
370+
Logger.Config.add_backend(backend)
371+
ok
372+
{:error, _} = error ->
373+
error
374+
end
373375
end
374376

375377
@doc """
376378
Removes a backend.
379+
380+
## Options
381+
382+
* `:flush` - when true, guarantees all messages currently sent
383+
to both Logger and Erlang's `error_logger` are processed before
384+
the backend is removed
377385
"""
378-
def remove_backend(backend) do
386+
def remove_backend(backend, opts \\ []) do
387+
_ = if opts[:flush], do: GenEvent.which_handlers(:error_logger)
388+
Logger.Config.remove_backend(backend)
379389
Logger.Watcher.unwatch(Logger, translate_backend(backend))
380390
end
381391

lib/logger/lib/logger/config.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ defmodule Logger.Config do
2222
GenEvent.call(Logger, @name, {:remove_translator, translator})
2323
end
2424

25+
def add_backend(backend) do
26+
GenEvent.call(Logger, @name, {:add_backend, backend})
27+
end
28+
29+
def remove_backend(backend) do
30+
GenEvent.call(Logger, @name, {:remove_backend, backend})
31+
end
32+
2533
def __data__() do
2634
Application.get_env(:logger, @data)
2735
end
@@ -88,8 +96,23 @@ defmodule Logger.Config do
8896
{:ok, :ok, state}
8997
end
9098

99+
def handle_call({:add_backend, backend}, state) do
100+
update_backends(&[backend|List.delete(&1, backend)])
101+
{:ok, :ok, state}
102+
end
103+
104+
def handle_call({:remove_backend, backend}, state) do
105+
update_backends(&List.delete(&1, backend))
106+
{:ok, :ok, state}
107+
end
108+
91109
## Helpers
92110

111+
defp update_backends(fun) do
112+
backends = fun.(Application.get_env(:logger, :backends, []))
113+
Application.put_env(:logger, :backends, backends)
114+
end
115+
93116
defp update_translators(%{translators: translators} = state, fun) do
94117
translators = fun.(translators)
95118
Application.put_env(:logger, :translators, translators)

lib/logger/test/logger_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule LoggerTest do
3232

3333
test "add_backend/1 and remove_backend/1" do
3434
assert :ok = Logger.remove_backend(:console)
35+
assert Application.get_env(:logger, :backends) == []
3536
assert Logger.remove_backend(:console) ==
3637
{:error, :module_not_found}
3738

@@ -40,8 +41,10 @@ defmodule LoggerTest do
4041
end) == ""
4142

4243
assert {:ok, pid} = Logger.add_backend(:console)
44+
assert Application.get_env(:logger, :backends) == [:console]
4345
assert Logger.add_backend(:console) ==
4446
{:error, {:already_started, pid}}
47+
assert Application.get_env(:logger, :backends) == [:console]
4548
end
4649

4750
test "add_backend/1 with {module, id}" do

0 commit comments

Comments
 (0)