memEx/lib/memex/logger.ex

64 lines
1.6 KiB
Elixir
Raw Permalink Normal View History

2023-02-07 00:18:08 -05:00
defmodule Memex.Logger do
2023-02-04 20:48:17 -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:18:08 -05:00
|> pretty_encode()
2023-02-04 20:48:17 -05:00
2023-04-13 23:29:29 -04:00
Logger.error("#{meta.reason} #{data}")
2023-02-04 20:48:17 -05:00
end
def handle_event([:oban, :job, :start], measure, meta, _config) do
2023-02-07 00:18:08 -05:00
data = get_oban_job_data(meta, measure) |> pretty_encode()
2023-04-13 23:29:29 -04:00
Logger.info("Started oban job: #{data}")
2023-02-04 20:48:17 -05:00
end
def handle_event([:oban, :job, :stop], measure, meta, _config) do
2023-02-07 00:18:08 -05:00
data = get_oban_job_data(meta, measure) |> pretty_encode()
2023-04-13 23:29:29 -04:00
Logger.info("Finished oban job: #{data}")
2023-02-04 20:48:17 -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:18:08 -05:00
|> pretty_encode()
2023-02-04 20:48:17 -05:00
2023-04-13 23:29:29 -04:00
Logger.warning("Unhandled oban job event: #{data}")
2023-02-04 20:48:17 -05:00
end
def handle_event(unhandled_event, measure, meta, config) do
2023-02-07 00:18:08 -05:00
data =
pretty_encode(%{
event: unhandled_event,
meta: meta,
measurements: measure,
config: config
})
2023-04-13 23:29:29 -04:00
Logger.warning("Unhandled telemetry event: #{data}")
2023-02-04 20:48:17 -05:00
end
defp get_oban_job_data(%{job: job}, measure) do
2023-02-07 00:18:08 -05:00
%{
job: job |> Map.take([:id, :args, :meta, :queue, :worker]),
measurements: measure
}
2023-02-04 20:48:17 -05:00
end
defp pretty_encode(data) do
data
|> Jason.encode!()
|> Jason.Formatter.pretty_print()
end
end