@@ -16,8 +16,10 @@ defmodule Logger do
16
16
performant when required but also apply backpressure
17
17
when under stress.
18
18
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.
21
23
22
24
Logging is useful for tracking when an event of interest happens in your
23
25
system. For example, it may be helpful to log whenever a user is deleted.
@@ -62,8 +64,8 @@ defmodule Logger do
62
64
* Runtime configuration - can be set before the `:logger`
63
65
application is started, but may be changed during runtime
64
66
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
67
69
68
70
### Application configuration
69
71
@@ -152,19 +154,24 @@ defmodule Logger do
152
154
### Error logger configuration
153
155
154
156
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.
157
159
158
160
* `: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`.
161
163
162
164
* `:handle_sasl_reports` - redirects supervisor, crash and
163
165
progress reports to `Logger` so they are formatted in Elixir
164
166
terms. Your application must guarantee `:sasl` is started before
165
167
`: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:
168
175
169
176
* `:discard_threshold_for_error_logger` - if `:error_logger` has more than
170
177
`discard_threshold` messages in its inbox, messages will be dropped
@@ -176,19 +183,17 @@ defmodule Logger do
176
183
350 (0.75 * threshold) entries and 50 (0.1 * theshold) messages will
177
184
be processed before the threshold is checked once again.
178
185
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:
182
188
183
189
config :logger,
184
190
handle_otp_reports: true,
185
191
handle_sasl_reports: true
186
192
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.
192
197
193
198
## Backends
194
199
@@ -523,7 +528,7 @@ defmodule Logger do
523
528
"""
524
529
@ spec flush :: :ok
525
530
def flush do
526
- _ = :gen_event . which_handlers ( :error_logger )
531
+ _ = Process . whereis ( :error_logger ) && :gen_event . which_handlers ( :error_logger )
527
532
:gen_event . sync_notify ( Logger , :flush )
528
533
end
529
534
@@ -533,8 +538,7 @@ defmodule Logger do
533
538
## Options
534
539
535
540
* `: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
538
542
539
543
"""
540
544
@ spec add_backend ( atom , keyword ) :: Supervisor . on_start_child ( )
@@ -560,8 +564,7 @@ defmodule Logger do
560
564
## Options
561
565
562
566
* `: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
565
568
566
569
"""
567
570
@ spec remove_backend ( atom , keyword ) :: :ok | { :error , term }
@@ -610,14 +613,17 @@ defmodule Logger do
610
613
when level in @ levels and is_list ( metadata ) do
611
614
case __metadata__ ( ) do
612
615
{ 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
619
626
truncated = truncate ( message , truncate )
620
-
621
627
tuple = { Logger , truncated , Logger.Utils . timestamp ( utc_log? ) , metadata }
622
628
623
629
try do
@@ -629,7 +635,7 @@ defmodule Logger do
629
635
:exit , reason -> { :error , reason }
630
636
end
631
637
else
632
- :ok
638
+ _ -> :ok
633
639
end
634
640
635
641
{ false , _ } ->
@@ -755,15 +761,17 @@ defmodule Logger do
755
761
end
756
762
757
763
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
759
769
end
760
770
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 }
763
773
end
764
774
765
- defp normalize_message ( message , metadata ) , do: { message , metadata }
766
-
767
775
defp truncate ( data , n ) when is_list ( data ) or is_binary ( data ) , do: Logger.Utils . truncate ( data , n )
768
776
defp truncate ( data , n ) , do: Logger.Utils . truncate ( to_string ( data ) , n )
769
777
0 commit comments