Skip to content

Commit

Permalink
Improve formatting of the crash reports
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDryga committed Sep 12, 2024
1 parent 09b7dc6 commit f1b0cc9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/logger_json/formatters/elastic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,31 @@ defmodule LoggerJSON.Formatters.Elastic do
|> maybe_put(:"error.code", get_exception_code(exception))
end

def format_crash_reason(message, {error, reason}, _meta) do
def format_crash_reason(message, {{{%type{} = exception, _}, _}, stacktrace}, _meta) do
formatted_stacktrace =
[
Exception.format_banner(:error, exception, stacktrace),
Exception.format_stacktrace(stacktrace)
]
|> Enum.join("\n")

format_error_fields(message, Exception.message(exception), formatted_stacktrace, type)
|> maybe_put(:"error.id", get_exception_id(exception))
|> maybe_put(:"error.code", get_exception_code(exception))
end

def format_crash_reason(message, {error, reason}, _meta) when is_atom(error) or is_binary(error) do
stacktrace = "** (#{error}) #{inspect(reason)}"
error_message = "#{error}: #{inspect(reason)}"
format_error_fields(message, error_message, stacktrace, error)
end

def format_crash_reason(message, {error, reason}, _meta) do
stacktrace = "** (#{inspect(error)}) #{inspect(reason)}"
error_message = "#{inspect(error)}: #{inspect(reason)}"
format_error_fields(message, error_message, stacktrace, "error")
end

defp get_exception_id(%{id: id}), do: id
defp get_exception_id(_), do: nil

Expand Down
11 changes: 11 additions & 0 deletions lib/logger_json/formatters/google_cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
format_reported_error_event(message, ruby_stacktrace, service_context, meta)
end

def format_crash_reason(message, {{{%{} = exception, _}, _}, stacktrace}, service_context, meta) do
ruby_stacktrace =
[
Exception.format_banner(:error, exception, stacktrace),
format_stacktrace(stacktrace)
]
|> Enum.join("\n")

format_reported_error_event(message, ruby_stacktrace, service_context, meta)
end

def format_crash_reason(binary, {error, reason}, service_context, meta) when is_atom(error) or is_binary(error) do
stacktrace = "** (#{error}) #{inspect(reason)}"
format_reported_error_event(binary, stacktrace, service_context, meta)
Expand Down
43 changes: 43 additions & 0 deletions test/logger_json/formatters/elastic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,49 @@ defmodule LoggerJSON.Formatters.ElasticTest do
assert String.starts_with?(stacktrace, "** (RuntimeError) oops")
end

test "logs Task/GenServer termination" do
test_pid = self()

logs =
capture_log(fn ->
{:ok, _} = Supervisor.start_link([{CrashingGenServer, :ok}], strategy: :one_for_one)

{:ok, _} =
Task.start(fn ->
try do
GenServer.call(CrashingGenServer, :boom)
catch
_ -> nil
after
send(test_pid, :done)
end
end)

# Wait for task to finish
receive do
:done -> nil
end

# Let logs flush
Process.sleep(100)
end)

[_, log_entry] =
logs
|> String.trim()
|> String.split("\n")
|> Enum.map(&decode_or_print_error/1)

assert %{
"error.message" => "boom",
"error.type" => "Elixir.RuntimeError",
"error.stack_trace" => "** (RuntimeError) boom" <> _,
"message" => message
} = log_entry

assert message =~ ~r/Task #PID<\d+.\d+.\d+> started from #{inspect(test_pid)} terminating/
end

test "passing options to encoder" do
formatter = {Elastic, encoder_opts: [pretty: true]}
:logger.update_handler_config(:default, :formatter, formatter)
Expand Down
2 changes: 1 addition & 1 deletion test/logger_json/formatters/google_cloud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ defmodule LoggerJSON.Formatters.GoogleCloudTest do
assert %{
"@type" => "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
"message" => message,
"stack_trace" => "** ({{%RuntimeError{message: \"boom\"}" <> _,
"stack_trace" => "** (RuntimeError) boom" <> _,
"serviceContext" => %{"service" => "nonode@nohost"}
} = log_entry

Expand Down

0 comments on commit f1b0cc9

Please sign in to comment.