Skip to content

Commit d45f451

Browse files
author
José Valim
committed
Allow configurable timeout and catch exits on logger
1 parent f1a3937 commit d45f451

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

lib/logger/lib/logger.ex

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ defmodule Logger do
6767
cause the message to be ignored. Keep in mind that each backend
6868
may have its specific level, too.
6969
70+
* `:timeout` - the timeout for sync requests in miliseconds, defaults
71+
to 5000 miliseconds.
72+
7073
* `:utc_log` - when `true`, uses UTC in logs. By default it uses
7174
local time (i.e. it defaults to `false`).
7275
@@ -364,19 +367,22 @@ defmodule Logger do
364367
Use this function only when there is a need to log dynamically
365368
or you want to explicitly avoid embedding metadata.
366369
"""
367-
@spec log(level, IO.chardata | (() -> IO.chardata), Keyword.t) :: :ok | {:error, :not_available}
370+
@spec log(level, IO.chardata | (() -> IO.chardata), Keyword.t) ::
371+
:ok | {:error, :noproc} | {:error, :timeout} | {:error, term}
368372
def log(level, chardata, metadata \\ []) when level in @levels and is_list(metadata) do
369-
%{mode: mode, truncate: truncate,
373+
%{mode: mode, truncate: truncate, timeout: timeout,
370374
level: min_level, utc_log: utc_log?} = Logger.Config.__data__
371375

372376
if compare_levels(level, min_level) != :lt do
373377
tuple = {Logger, truncate(chardata, truncate), Logger.Utils.timestamp(utc_log?),
374378
[pid: self()] ++ metadata() ++ metadata}
375379
try do
376-
notify(mode, {level, Process.group_leader(), tuple})
380+
notify(mode, {level, Process.group_leader(), tuple}, timeout)
377381
:ok
378382
rescue
379-
ArgumentError -> {:error, :not_available}
383+
ArgumentError -> {:error, :noproc}
384+
catch
385+
:exit, {reason, _} -> {:error, reason}
380386
end
381387
else
382388
:ok
@@ -453,6 +459,6 @@ defmodule Logger do
453459
defp truncate(data, n) when is_list(data) or is_binary(data),
454460
do: Logger.Utils.truncate(data, n)
455461

456-
defp notify(:sync, msg), do: GenEvent.sync_notify(Logger, msg)
457-
defp notify(:async, msg), do: GenEvent.notify(Logger, msg)
462+
defp notify(:sync, msg, timeout), do: GenEvent.sync_notify(Logger, msg, timeout)
463+
defp notify(:async, msg, _timeout), do: GenEvent.notify(Logger, msg)
458464
end

lib/logger/lib/logger/config.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,15 @@ defmodule Logger.Config do
134134

135135
defp compute_state(mode) do
136136
level = Application.get_env(:logger, :level)
137+
timeout = Application.get_env(:logger, :timeout)
137138
utc_log = Application.get_env(:logger, :utc_log)
138139
truncate = Application.get_env(:logger, :truncate)
139140
translators = Application.get_env(:logger, :translators)
140141

141142
sync_threshold = Application.get_env(:logger, :sync_threshold)
142143
async_threshold = trunc(sync_threshold * 0.75)
143144

144-
persist %{level: level, mode: mode, truncate: truncate,
145+
persist %{level: level, mode: mode, timeout: timeout, truncate: truncate,
145146
utc_log: utc_log, sync_threshold: sync_threshold,
146147
async_threshold: async_threshold, translators: translators}
147148
end

lib/logger/mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule Logger.Mixfile do
1111
[registered: [Logger, Logger.Supervisor, Logger.Watcher],
1212
mod: {Logger.App, []},
1313
env: [level: :debug,
14+
timeout: 5000,
1415
utc_log: false,
1516
truncate: 8096,
1617
backends: [:console],

lib/logger/test/logger_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ defmodule LoggerTest do
161161
Process.unregister(Logger)
162162

163163
try do
164-
assert Logger.log(:debug, "hello") == {:error, :not_available}
164+
assert Logger.log(:debug, "hello") == {:error, :noproc}
165165
after
166166
Process.register(logger, Logger)
167167
end

0 commit comments

Comments
 (0)