Skip to content

Commit

Permalink
feat: workaround for missing Logger messages
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Feb 11, 2025
1 parent 260a1bc commit 0fae412
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
27 changes: 27 additions & 0 deletions lib/tower_error_tracker/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ defmodule TowerErrorTracker.Reporter do
:ok
end

def report_event(
%Tower.Event{
kind: :message,
reason: reason,
level: level,
stacktrace: stacktrace,
log_event: %{meta: %{mfa: {m, f, a}, file: file, line: line}}
} = event
)
when is_nil(stacktrace) or length(stacktrace) == 0 do
ErrorTracker.report(
{:message, "[#{level}] #{reason}"},
[{m, f, a, [file: file, line: line]}],
context(event)
)

:ok
end

def report_event(
%Tower.Event{kind: :message, reason: reason, level: level, stacktrace: stacktrace} = event
) do
ErrorTracker.report({:message, "[#{level}] #{reason}"}, stacktrace || [], context(event))

:ok
end

def report_event(_event) do
:ignore
end
Expand Down
21 changes: 18 additions & 3 deletions test/tower_error_tracker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,31 @@ defmodule TowerErrorTrackerTest do
)
end

test "Logger messages not reported because not supported by ErrorTracker" do
test "Logger messages reported as special custom exceptions (because messages not supported by ErrorTracker)" do
in_unlinked_process(fn ->
require Logger

capture_log(fn ->
Logger.emergency("Panic!")
Logger.emergency("Emergency!")

Logger.critical("Critical!")
end)
end)

assert [] = TestApp.Repo.all(ErrorTracker.Error) |> TestApp.Repo.preload(:occurrences)
assert_eventually(
[
%{
kind: "message",
reason: "[emergency] Emergency!",
occurrences: [_]
},
%{
kind: "message",
reason: "[critical] Critical!",
occurrences: [_]
}
] = TestApp.Repo.all(ErrorTracker.Error) |> TestApp.Repo.preload(:occurrences)
)
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
Expand Down

0 comments on commit 0fae412

Please sign in to comment.