cannery/lib/lokal/logger.ex

64 lines
1.6 KiB
Elixir
Raw Normal View History

2023-02-07 00:14:20 -05:00
defmodule Lokal.Logger do
2023-02-04 20:47:47 -05:00
@moduledoc """
Custom logger for telemetry events
Oban implementation taken from
https://hexdocs.pm/oban/Oban.html#module-reporting-errors
"""
require Logger
def handle_event([:oban, :job, :exception], measure, %{stacktrace: stacktrace} = meta, _config) do
data =
get_oban_job_data(meta, measure)
|> Map.put(:stacktrace, Exception.format_stacktrace(stacktrace))
2023-02-07 00:14:20 -05:00
|> pretty_encode()
2023-02-04 20:47:47 -05:00
2023-02-07 00:14:20 -05:00
Logger.error(meta.reason, data: data)
2023-02-04 20:47:47 -05:00
end
def handle_event([:oban, :job, :start], measure, meta, _config) do
2023-02-07 00:14:20 -05:00
data = get_oban_job_data(meta, measure) |> pretty_encode()
Logger.info("Started oban job", data: data)
2023-02-04 20:47:47 -05:00
end
def handle_event([:oban, :job, :stop], measure, meta, _config) do
2023-02-07 00:14:20 -05:00
data = get_oban_job_data(meta, measure) |> pretty_encode()
Logger.info("Finished oban job", data: data)
2023-02-04 20:47:47 -05:00
end
def handle_event([:oban, :job, unhandled_event], measure, meta, _config) do
data =
get_oban_job_data(meta, measure)
|> Map.put(:event, unhandled_event)
2023-02-07 00:14:20 -05:00
|> pretty_encode()
2023-02-04 20:47:47 -05:00
2023-02-07 00:14:20 -05:00
Logger.warning("Unhandled oban job event", data: data)
2023-02-04 20:47:47 -05:00
end
def handle_event(unhandled_event, measure, meta, config) do
2023-02-07 00:14:20 -05:00
data =
pretty_encode(%{
event: unhandled_event,
meta: meta,
measurements: measure,
config: config
})
Logger.warning("Unhandled telemetry event", data: data)
2023-02-04 20:47:47 -05:00
end
defp get_oban_job_data(%{job: job}, measure) do
2023-02-07 00:14:20 -05:00
%{
job: job |> Map.take([:id, :args, :meta, :queue, :worker]),
measurements: measure
}
2023-02-04 20:47:47 -05:00
end
defp pretty_encode(data) do
data
|> Jason.encode!()
|> Jason.Formatter.pretty_print()
end
end