Skip to content

Commit 3102684

Browse files
authored
Expose :logger's global metadata (#12413)
1 parent 5a57c26 commit 3102684

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

lib/logger/lib/logger.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ defmodule Logger do
191191
should be logged. Defaults to `false`. This option only has an effect
192192
if `:handle_otp_reports` is true.
193193
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+
194198
For example, to configure `Logger` to redirect all Erlang messages using a
195199
`config/config.exs` file:
196200
@@ -452,6 +456,9 @@ defmodule Logger do
452456

453457
@doc """
454458
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.
455462
"""
456463
@spec metadata() :: metadata
457464
def metadata() do

lib/logger/lib/logger/app.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ defmodule Logger.App do
3434
primary_config = :logger.get_primary_config()
3535
:ok = :logger.set_primary_config(:level, default_level())
3636

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+
3743
process_level_filter = {&Logger.Utils.process_level/2, []}
3844
:ok = :logger.add_primary_filter(:logger_process_level, process_level_filter)
3945

lib/logger/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ defmodule Logger.MixProject do
2525
start_options: [],
2626
sync_threshold: 20,
2727
discard_threshold: 500,
28-
discard_threshold_periodic_check: 30_000
28+
discard_threshold_periodic_check: 30_000,
29+
metadata: []
2930
]
3031
]
3132
end

lib/logger/test/logger_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,17 @@ defmodule LoggerTest do
582582
Application.start(:logger)
583583
end
584584

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+
585596
test "writes to stderr on bad default handler config" do
586597
Application.put_env(:logger, :default_handler, config: %{file: 123})
587598
Logger.App.stop()

0 commit comments

Comments
 (0)