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 6, 2025
1 parent 260a1bc commit 4ca8247
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/tower_error_tracker/reporter.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
defmodule TowerErrorTracker.ReportedMessage do
defexception [:level, :message]

def message(%{level: level, message: message}) do
"[#{level}] #{message}"
end
end

defmodule TowerErrorTracker.Reporter do
@moduledoc false

Expand All @@ -19,6 +27,13 @@ defmodule TowerErrorTracker.Reporter do
:ok
end

def report_event(%Tower.Event{kind: :message, reason: reason, level: level} = event) do
TowerErrorTracker.ReportedMessage.exception(level: level, message: reason)
|> ErrorTracker.report([], context(event))

:ok
end

def report_event(_event) do
:ignore
end
Expand Down
12 changes: 10 additions & 2 deletions test/tower_error_tracker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ 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

Expand All @@ -228,7 +228,15 @@ defmodule TowerErrorTrackerTest do
end)
end)

assert [] = TestApp.Repo.all(ErrorTracker.Error) |> TestApp.Repo.preload(:occurrences)
assert_eventually(
[
%{
kind: "Elixir.TowerErrorTracker.ReportedMessage",
reason: "[emergency] Panic!",
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 4ca8247

Please sign in to comment.