From 56dae6cdfed55297e1facae3e7440a05d93d2c63 Mon Sep 17 00:00:00 2001 From: shibao Date: Tue, 7 Feb 2023 00:14:20 -0500 Subject: [PATCH] improve logger --- lib/lokal/application.ex | 4 +-- lib/lokal/{error_reporter.ex => logger.ex} | 36 +++++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) rename lib/lokal/{error_reporter.ex => logger.ex} (57%) diff --git a/lib/lokal/application.ex b/lib/lokal/application.ex index e63bdbd7..93f96441 100644 --- a/lib/lokal/application.ex +++ b/lib/lokal/application.ex @@ -4,7 +4,7 @@ defmodule Lokal.Application do @moduledoc false use Application - alias Lokal.ErrorReporter + alias Lokal.Logger @impl true def start(_type, _args) do @@ -33,7 +33,7 @@ defmodule Lokal.Application do [:oban, :job, :start], [:oban, :job, :stop] ], - &ErrorReporter.handle_event/4, + &Logger.handle_event/4, [] ) diff --git a/lib/lokal/error_reporter.ex b/lib/lokal/logger.ex similarity index 57% rename from lib/lokal/error_reporter.ex rename to lib/lokal/logger.ex index d46c5552..0e32b43d 100644 --- a/lib/lokal/error_reporter.ex +++ b/lib/lokal/logger.ex @@ -1,4 +1,4 @@ -defmodule Lokal.ErrorReporter do +defmodule Lokal.Logger do @moduledoc """ Custom logger for telemetry events @@ -12,41 +12,47 @@ defmodule Lokal.ErrorReporter do data = get_oban_job_data(meta, measure) |> Map.put(:stacktrace, Exception.format_stacktrace(stacktrace)) + |> pretty_encode() - Logger.error(meta.reason, data: pretty_encode(data)) + Logger.error(meta.reason, data: data) end def handle_event([:oban, :job, :start], measure, meta, _config) do - Logger.info("Started oban job", data: get_oban_job_data(meta, measure) |> pretty_encode()) + data = get_oban_job_data(meta, measure) |> pretty_encode() + Logger.info("Started oban job", data: data) end def handle_event([:oban, :job, :stop], measure, meta, _config) do - Logger.info("Finished oban job", data: get_oban_job_data(meta, measure) |> pretty_encode()) + data = get_oban_job_data(meta, measure) |> pretty_encode() + Logger.info("Finished oban job", data: data) end def handle_event([:oban, :job, unhandled_event], measure, meta, _config) do data = get_oban_job_data(meta, measure) |> Map.put(:event, unhandled_event) + |> pretty_encode() - Logger.warning("Unhandled oban job event", data: pretty_encode(data)) + Logger.warning("Unhandled oban job event", data: data) end def handle_event(unhandled_event, measure, meta, config) do - data = %{ - event: unhandled_event, - meta: meta, - measurements: measure, - config: config - } + data = + pretty_encode(%{ + event: unhandled_event, + meta: meta, + measurements: measure, + config: config + }) - Logger.warning("Unhandled telemetry event", data: pretty_encode(data)) + Logger.warning("Unhandled telemetry event", data: data) end defp get_oban_job_data(%{job: job}, measure) do - job - |> Map.take([:id, :args, :meta, :queue, :worker]) - |> Map.merge(measure) + %{ + job: job |> Map.take([:id, :args, :meta, :queue, :worker]), + measurements: measure + } end defp pretty_encode(data) do