-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic error handling/capturing (#3)
- Loading branch information
Showing
4 changed files
with
282 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Tower.EphemeralReporter do | ||
use Agent | ||
|
||
def start_link(_opts) do | ||
Agent.start_link(fn -> [] end, name: __MODULE__) | ||
end | ||
|
||
def report_exception(exception, stacktrace, meta \\ %{}) | ||
when is_exception(exception) and is_list(stacktrace) do | ||
Agent.update( | ||
__MODULE__, | ||
fn errors -> | ||
[ | ||
%{ | ||
time: Map.get(meta, :time, :logger.timestamp()), | ||
type: exception.__struct__, | ||
reason: Exception.message(exception), | ||
stacktrace: stacktrace | ||
} | ||
| errors | ||
] | ||
end | ||
) | ||
end | ||
|
||
def report(type, reason, stacktrace, meta \\ %{}) when is_atom(type) and is_list(stacktrace) do | ||
Agent.update( | ||
__MODULE__, | ||
fn errors -> | ||
[ | ||
%{ | ||
time: Map.get(meta, :time, :logger.timestamp()), | ||
type: type, | ||
reason: reason, | ||
stacktrace: stacktrace | ||
} | ||
| errors | ||
] | ||
end | ||
) | ||
end | ||
|
||
def errors do | ||
Agent.get(__MODULE__, & &1) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Tower.LoggerHandler do | ||
@default_level :error | ||
@handler_id :tower | ||
|
||
def attach do | ||
:logger.add_handler(@handler_id, __MODULE__, %{level: @default_level}) | ||
end | ||
|
||
def detach do | ||
:logger.remove_handler(@handler_id) | ||
end | ||
|
||
# :logger callbacks | ||
|
||
def adding_handler(config) do | ||
IO.puts("[Tower.LoggerHandler] ADDING config=#{inspect(config)}") | ||
|
||
{:ok, config} | ||
end | ||
|
||
def removing_handler(config) do | ||
IO.puts("[Tower.LoggerHandler] REMOVING config=#{inspect(config)}") | ||
|
||
:ok | ||
end | ||
|
||
def log(%{level: :error, meta: %{crash_reason: {exception, stacktrace}} = meta}, _config) | ||
when is_exception(exception) and is_list(stacktrace) do | ||
IO.puts("[Tower.LoggerHandler] EXCEPTION #{inspect(exception)}") | ||
|
||
Tower.report_exception(exception, stacktrace, meta) | ||
end | ||
|
||
def log( | ||
%{level: :error, meta: %{crash_reason: {{:nocatch, reason}, stacktrace}} = meta}, | ||
_config | ||
) | ||
when is_list(stacktrace) do | ||
IO.puts("[Tower.LoggerHandler] NOCATCH #{inspect(reason)}") | ||
|
||
Tower.report(:nocatch, reason, stacktrace, meta) | ||
end | ||
|
||
def log(%{level: :error, meta: %{crash_reason: {exit_reason, stacktrace}} = meta}, _config) | ||
when is_list(stacktrace) do | ||
IO.puts("[Tower.LoggerHandler] EXIT #{inspect(exit_reason)}") | ||
|
||
Tower.report(:exit, exit_reason, stacktrace, meta) | ||
end | ||
|
||
def log(log_event, _config) do | ||
IO.puts( | ||
"[Tower.LoggerHandler] UNHANDLED LOG EVENT log_event=#{inspect(log_event, pretty: true)}" | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters