@@ -16,8 +16,10 @@ defmodule Logger do
1616 performant when required but also apply backpressure
1717 when under stress.
1818
19- * Wraps OTP's [`:error_logger`](http://erlang.org/doc/man/error_logger.html)
20- to prevent it from overflowing.
19+ * Plugs into Erlang's [`:logger`](http://erlang.org/doc/man/logger.html)
20+ (from Erlang/OTP 21) to convert terms to Elixir syntax or wraps
21+ Erlang's [`:error_logger`](http://erlang.org/doc/man/error_logger.html)
22+ in earlier Erlang/OTP versions to prevent it from overflowing.
2123
2224 Logging is useful for tracking when an event of interest happens in your
2325 system. For example, it may be helpful to log whenever a user is deleted.
@@ -62,8 +64,8 @@ defmodule Logger do
6264 * Runtime configuration - can be set before the `:logger`
6365 application is started, but may be changed during runtime
6466
65- * Error logger configuration - configuration for the
66- wrapper around OTP 's [`:error_logger`](http://erlang.org/doc/man/error_logger.html)
67+ * Erlang configuration - options that handle integration with
68+ Erlang 's logging facilities
6769
6870 ### Application configuration
6971
@@ -152,19 +154,24 @@ defmodule Logger do
152154 ### Error logger configuration
153155
154156 The following configuration applies to `Logger`'s wrapper around
155- OTP 's [`:error_logger`](http://erlang.org/doc/man/error_logger.html).
156- All the configurations below must be set before the `:logger` application starts.
157+ Erlang 's logging functionalities. All the configurations below must
158+ be set before the `:logger` application starts.
157159
158160 * `:handle_otp_reports` - redirects OTP reports to `Logger` so
159- they are formatted in Elixir terms. This uninstalls OTP's
160- logger that prints terms to terminal . Defaults to `true`.
161+ they are formatted in Elixir terms. This effectively disables
162+ Erlang standard logger . Defaults to `true`.
161163
162164 * `:handle_sasl_reports` - redirects supervisor, crash and
163165 progress reports to `Logger` so they are formatted in Elixir
164166 terms. Your application must guarantee `:sasl` is started before
165167 `:logger`. This means you may see some initial reports written
166- in Erlang syntax until the Logger application kicks in and
167- uninstalls SASL's logger in favor of its own. Defaults to `false`.
168+ in Erlang syntax until the Logger application kicks in.
169+ Defaults to `false`.
170+
171+ From Erlang/OTP 21, `:handle_sasl_reports` only has an effect if
172+ `:handle_otp_reports` is true.
173+
174+ The following configurations apply only for Erlang/OTP 20 and earlier:
168175
169176 * `:discard_threshold_for_error_logger` - if `:error_logger` has more than
170177 `discard_threshold` messages in its inbox, messages will be dropped
@@ -176,19 +183,17 @@ defmodule Logger do
176183 350 (0.75 * threshold) entries and 50 (0.1 * theshold) messages will
177184 be processed before the threshold is checked once again.
178185
179- For example, to configure `Logger` to redirect all
180- [`:error_logger`](http://erlang.org/doc/man/error_logger.html) messages
181- using a `config/config.exs` file:
186+ For example, to configure `Logger` to redirect all Erlang messages using a
187+ `config/config.exs` file:
182188
183189 config :logger,
184190 handle_otp_reports: true,
185191 handle_sasl_reports: true
186192
187- Furthermore, `Logger` allows messages sent by OTP's `:error_logger`
188- to be translated into an Elixir format via translators. Translators
189- can be dynamically added at any time with the `add_translator/1`
190- and `remove_translator/1` APIs. Check `Logger.Translator` for more
191- information.
193+ Furthermore, `Logger` allows messages sent by Erlang to be translated
194+ into an Elixir format via translators. Translators can be added at any
195+ time with the `add_translator/1` and `remove_translator/1` APIs. Check
196+ `Logger.Translator` for more information.
192197
193198 ## Backends
194199
@@ -523,7 +528,7 @@ defmodule Logger do
523528 """
524529 @ spec flush :: :ok
525530 def flush do
526- _ = :gen_event . which_handlers ( :error_logger )
531+ _ = Process . whereis ( :error_logger ) && :gen_event . which_handlers ( :error_logger )
527532 :gen_event . sync_notify ( Logger , :flush )
528533 end
529534
@@ -533,8 +538,7 @@ defmodule Logger do
533538 ## Options
534539
535540 * `:flush` - when `true`, guarantees all messages currently sent
536- to both Logger and OTP's [`:error_logger`](http://erlang.org/doc/man/error_logger.html)
537- are processed before the backend is added
541+ to `Logger` are processed before the backend is added
538542
539543 """
540544 @ spec add_backend ( atom , keyword ) :: Supervisor . on_start_child ( )
@@ -560,8 +564,7 @@ defmodule Logger do
560564 ## Options
561565
562566 * `:flush` - when `true`, guarantees all messages currently sent
563- to both Logger and OTP's [`:error_logger`](http://erlang.org/doc/man/error_logger.html)
564- are processed before the backend is removed
567+ to `Logger` are processed before the backend is removed
565568
566569 """
567570 @ spec remove_backend ( atom , keyword ) :: :ok | { :error , term }
@@ -610,14 +613,17 @@ defmodule Logger do
610613 when level in @ levels and is_list ( metadata ) do
611614 case __metadata__ ( ) do
612615 { true , pdict } ->
613- % { mode: mode , truncate: truncate , level: min_level , utc_log: utc_log? } =
614- Logger.Config . __data__ ( )
615-
616- if compare_levels ( level , min_level ) != :lt and mode != :discard do
617- metadata = [ pid: self ( ) ] ++ Keyword . merge ( pdict , metadata )
618- { message , metadata } = normalize_message ( chardata_or_fun , metadata )
616+ % {
617+ mode: mode ,
618+ truncate: truncate ,
619+ level: min_level ,
620+ utc_log: utc_log?
621+ } = Logger.Config . __data__ ( )
622+
623+ with true <- compare_levels ( level , min_level ) != :lt and mode != :discard ,
624+ metadata = [ pid: self ( ) ] ++ Keyword . merge ( pdict , metadata ) ,
625+ { message , metadata } <- normalize_message ( chardata_or_fun , metadata ) do
619626 truncated = truncate ( message , truncate )
620-
621627 tuple = { Logger , truncated , Logger.Utils . timestamp ( utc_log? ) , metadata }
622628
623629 try do
@@ -629,7 +635,7 @@ defmodule Logger do
629635 :exit , reason -> { :error , reason }
630636 end
631637 else
632- :ok
638+ _ -> :ok
633639 end
634640
635641 { false , _ } ->
@@ -755,15 +761,17 @@ defmodule Logger do
755761 end
756762
757763 defp normalize_message ( fun , metadata ) when is_function ( fun , 0 ) do
758- normalize_message ( fun . ( ) , metadata )
764+ case fun . ( ) do
765+ { message , fun_metadata } -> { message , Keyword . merge ( metadata , fun_metadata ) }
766+ :skip -> :skip
767+ message -> { message , metadata }
768+ end
759769 end
760770
761- defp normalize_message ( { message , fun_metadata } , metadata ) when is_list ( fun_metadata ) do
762- { message , Keyword . merge ( metadata , fun_metadata ) }
771+ defp normalize_message ( message , metadata ) do
772+ { message , metadata }
763773 end
764774
765- defp normalize_message ( message , metadata ) , do: { message , metadata }
766-
767775 defp truncate ( data , n ) when is_list ( data ) or is_binary ( data ) , do: Logger.Utils . truncate ( data , n )
768776 defp truncate ( data , n ) , do: Logger.Utils . truncate ( to_string ( data ) , n )
769777
0 commit comments