Packages
significa_utils
0.3.1
0.3.2
0.3.1
0.3.0
0.3.0-1
0.2.0
0.1.0
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
0.0.1-main-d418c86c2c8e6d2638e4a225893f6e068bf36270
0.0.1-main-a68c9941c5d644aa896c6919c6ccfc2aa420ea48
0.0.1-main-9b7a22dadf92316ec0551664b2afd2687c6d5815
0.0.1-main-72a387d58ff1307364683940dbea968462c39c1d-1
0.0.1-main-604c9394407686ac217f507d69140a39c1198e3b-1
Collection of elixir utils
Current section
Files
Jump to
Current section
Files
lib/logger_metadata.ex
defmodule SignificaUtils.LoggerMetadata do
@moduledoc """
Utility for managing logger metadata in various contexts.
Provides functions to set logger metadata for improved consistent metadata across logging
and error tracking.
"""
require Logger
@doc """
Sets logger metadata, if Sentry is available also sets Sentry extra context.
## Parameters
- metadata: A keyword list of metadata key-value pairs
## Examples
iex> SignificaUtils.LoggerMetadata.set_metadata(request_id: "abc123", user_id: 456)
:ok
"""
def set_metadata(metadata) do
Logger.metadata(metadata)
quote do
if Code.ensure_loaded?(Sentry.Context) do
unquote(Sentry.Context.set_extra_context(Map.new(metadata)))
end
end
:ok
end
if Code.ensure_loaded?(Oban.Job) do
@doc """
Sets up logger metadata for an Oban job.
Takes a job struct with id and worker fields and sets the logger metadata
with the job_id and worker name.
## Examples
iex> job = %Oban.Job{id: 123, worker: "SampleApp.ExampleWorker"}
iex> SignificaUtils.LoggerMetadata.set_oban_job_metadata(job)
:ok
"""
def set_oban_job_metadata(
%Oban.Job{id: job_id, worker: worker},
extra \\ []
) do
[
job_id: job_id,
worker: worker
]
|> Keyword.merge(extra)
|> set_metadata()
end
end
end