Skip to content

Commit

Permalink
Accept structs as structured logs in Sentry.LoggerHandler (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdotb authored Sep 2, 2024
1 parent 116140d commit bcdcc04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/sentry/logger_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ defmodule Sentry.LoggerHandler do
log_from_crash_reason(log_event.meta[:crash_reason], unicode_chardata, sentry_opts, config)
end

# "report" here is of type logger:report/0, which is a map or keyword list.
# "report" here is of type logger:report/0, which is a struct, map or keyword list.
defp log_unfiltered(%{msg: {:report, report}}, sentry_opts, %__MODULE__{} = config)
when is_struct(report) do
capture(:message, inspect(report), sentry_opts, config)
end

defp log_unfiltered(%{msg: {:report, report}}, sentry_opts, %__MODULE__{} = config) do
case Map.new(report) do
%{reason: {exception, stacktrace}}
Expand Down
24 changes: 23 additions & 1 deletion test/sentry/logger_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ defmodule Sentry.LoggerHandlerTest do
end

@tag handler_config: %{capture_log_messages: true}
test "support structured logs", %{sender_ref: ref} do
test "support structured logs keyword", %{sender_ref: ref} do
Logger.error(foo: "bar")

assert_receive {^ref, event}
Expand All @@ -145,6 +145,28 @@ defmodule Sentry.LoggerHandlerTest do
refute_received {^ref, _event}, 100
end

test "support structured logs map", %{sender_ref: ref} do
Logger.error(%{foo: "bar"})

assert_receive {^ref, event}
assert event.message.formatted == "%{foo: \"bar\"}"

refute_received {^ref, _event}, 100
end

defmodule Foo do
defstruct [:bar]
end

test "support structured logs struct", %{sender_ref: ref} do
Logger.error(%Foo{})

assert_receive {^ref, event}
assert event.message.formatted == "%Sentry.LoggerHandlerTest.Foo{bar: nil}"

refute_received {^ref, _event}, 100
end

@tag handler_config: %{capture_log_messages: true, level: :warning}
test "respects the configured :level", %{sender_ref: ref} do
Logger.log(:warning, "Testing warning")
Expand Down

0 comments on commit bcdcc04

Please sign in to comment.