File tree Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,10 @@ defmodule Logger do
191
191
should be logged. Defaults to `false`. This option only has an effect
192
192
if `:handle_otp_reports` is true.
193
193
194
+ * `:metadata` - global primary metadata to be included in all log messages.
195
+ Defaults to `[]`. This can be overridden at the process level with `metadata/1`
196
+ or each on log call as desired.
197
+
194
198
For example, to configure `Logger` to redirect all Erlang messages using a
195
199
`config/config.exs` file:
196
200
@@ -452,6 +456,9 @@ defmodule Logger do
452
456
453
457
@ doc """
454
458
Reads the current process metadata.
459
+
460
+ This does not return the "global" logger metadata (set via the `:metadata` key in the
461
+ `:logger` application config), but only the process metadata.
455
462
"""
456
463
@ spec metadata ( ) :: metadata
457
464
def metadata ( ) do
Original file line number Diff line number Diff line change @@ -34,6 +34,12 @@ defmodule Logger.App do
34
34
primary_config = :logger . get_primary_config ( )
35
35
:ok = :logger . set_primary_config ( :level , default_level ( ) )
36
36
37
+ # If there is additional metadata in the :logger config, we merge it into
38
+ # the primary :logger metadata.
39
+ with [ _ | _ ] = metadata <- Application . fetch_env! ( :logger , :metadata ) do
40
+ :ok = :logger . set_primary_config ( :metadata , Enum . into ( metadata , primary_config . metadata ) )
41
+ end
42
+
37
43
process_level_filter = { & Logger.Utils . process_level / 2 , [ ] }
38
44
:ok = :logger . add_primary_filter ( :logger_process_level , process_level_filter )
39
45
Original file line number Diff line number Diff line change @@ -25,7 +25,8 @@ defmodule Logger.MixProject do
25
25
start_options: [ ] ,
26
26
sync_threshold: 20 ,
27
27
discard_threshold: 500 ,
28
- discard_threshold_periodic_check: 30_000
28
+ discard_threshold_periodic_check: 30_000 ,
29
+ metadata: [ ]
29
30
]
30
31
]
31
32
end
Original file line number Diff line number Diff line change @@ -582,6 +582,17 @@ defmodule LoggerTest do
582
582
Application . start ( :logger )
583
583
end
584
584
585
+ test "starts the application with global metadata" do
586
+ Application . put_env ( :logger , :metadata , global_meta: :yes )
587
+ Logger.App . stop ( )
588
+ Application . start ( :logger )
589
+ assert :logger . get_primary_config ( ) [ :metadata ] == % { global_meta: :yes }
590
+ after
591
+ Application . put_env ( :logger , :metadata , [ ] )
592
+ Logger.App . stop ( )
593
+ Application . start ( :logger )
594
+ end
595
+
585
596
test "writes to stderr on bad default handler config" do
586
597
Application . put_env ( :logger , :default_handler , config: % { file: 123 } )
587
598
Logger.App . stop ( )
You can’t perform that action at this time.
0 commit comments