defmodule Logger.Backends.ExRollbar do @moduledoc """ Logger backend that forwards `Logger.Error/2` to Rollbar """ use GenEvent import ExRollbar, only: [report: 4] def init(_) do {:ok, []} end def handle_event({level, _, {Logger, message, timestamp, metadata}}, state) when level in [:error, :warn] do if Keyword.get(metadata, :rollbar, true) == false do {:ok, state} else unix_ts = make_unix_ts(timestamp) meta = Keyword.put(metadata, :timestamp, unix_ts) module = Keyword.get(meta, :module, :no_module) || :no_module {function, arity} = Keyword.get(meta, :function, :no_fun) |> parse_fun file = Keyword.get(meta, :file, :no_file) line = Keyword.get(meta, :line, :no_line) synth_stack = [ {module, function, arity, [file: file, line: line]} ] report(level, message, synth_stack, meta) {:ok, state} end end def handle_event(_, state), do: {:ok, state} defp parse_fun(nil), do: {:no_fun, :no_arity} defp parse_fun(fun) when is_atom(fun), do: {fun, :no_arity} defp parse_fun(str) when is_binary(str) do [name, str_int] = String.split(str, "/") {String.to_atom(name), String.to_integer(str_int)} end defp make_unix_ts(_), do: :os.system_time(:seconds) end