Skip to content

Expose :logger's global metadata #12413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/logger/lib/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ defmodule Logger do
should be logged. Defaults to `false`. This option only has an effect
if `:handle_otp_reports` is true.

* `:metadata` - global primary metadata to be included in all log messages.
Defaults to `[]`. This can be overridden at the process level with `metadata/1`
or each on log call as desired.

For example, to configure `Logger` to redirect all Erlang messages using a
`config/config.exs` file:

Expand Down Expand Up @@ -452,6 +456,9 @@ defmodule Logger do

@doc """
Reads the current process metadata.

This does not return the "global" logger metadata (set via the `:metadata` key in the
`:logger` application config), but only the process metadata.
"""
@spec metadata() :: metadata
def metadata() do
Expand Down
6 changes: 6 additions & 0 deletions lib/logger/lib/logger/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ defmodule Logger.App do
primary_config = :logger.get_primary_config()
:ok = :logger.set_primary_config(:level, default_level())

# If there is additional metadata in the :logger config, we merge it into
# the primary :logger metadata.
with [_ | _] = metadata <- Application.fetch_env!(:logger, :metadata) do
:ok = :logger.set_primary_config(:metadata, Enum.into(metadata, primary_config.metadata))
end

process_level_filter = {&Logger.Utils.process_level/2, []}
:ok = :logger.add_primary_filter(:logger_process_level, process_level_filter)

Expand Down
3 changes: 2 additions & 1 deletion lib/logger/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Logger.MixProject do
start_options: [],
sync_threshold: 20,
discard_threshold: 500,
discard_threshold_periodic_check: 30_000
discard_threshold_periodic_check: 30_000,
metadata: []
]
]
end
Expand Down
11 changes: 11 additions & 0 deletions lib/logger/test/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,17 @@ defmodule LoggerTest do
Application.start(:logger)
end

test "starts the application with global metadata" do
Application.put_env(:logger, :metadata, global_meta: :yes)
Logger.App.stop()
Application.start(:logger)
assert :logger.get_primary_config()[:metadata] == %{global_meta: :yes}
after
Application.put_env(:logger, :metadata, [])
Logger.App.stop()
Application.start(:logger)
end

test "writes to stderr on bad default handler config" do
Application.put_env(:logger, :default_handler, config: %{file: 123})
Logger.App.stop()
Expand Down