diff --git a/lib/tower/ephemeral_reporter.ex b/lib/tower/ephemeral_reporter.ex index 57cc7d0..c274a56 100644 --- a/lib/tower/ephemeral_reporter.ex +++ b/lib/tower/ephemeral_reporter.ex @@ -3,6 +3,28 @@ defmodule Tower.EphemeralReporter do A very slim and naive built-in reporter, that just stores Tower events as process state. Possibly useful for development or testing. + + ## Example + + iex> {:ok, pid} = Tower.EphemeralReporter.start_link([]) + iex> Tower.EphemeralReporter.events() + [] + iex> try do + ...> 1 / 0 + ...> catch + ...> kind, reason -> + ...> Tower.handle_caught(kind, reason, __STACKTRACE__) + ...> end + :ok + iex> Process.sleep(200) + :ok + iex> [event] = Tower.EphemeralReporter.events() + iex> event.kind + :error + iex> event.reason + %ArithmeticError{message: "bad argument in arithmetic expression"} + iex> Tower.EphemeralReporter.stop(pid) + :ok """ @behaviour Tower.Reporter @@ -16,6 +38,10 @@ defmodule Tower.EphemeralReporter do Agent.start_link(fn -> [] end, name: __MODULE__) end + def stop(pid) do + Agent.stop(pid) + end + @impl true def report_event(%Event{level: level} = event) do if Tower.equal_or_greater_level?(level, @default_level) do diff --git a/lib/tower/reporter.ex b/lib/tower/reporter.ex index 7aaea80..15d74f0 100644 --- a/lib/tower/reporter.ex +++ b/lib/tower/reporter.ex @@ -1,6 +1,9 @@ defmodule Tower.Reporter do @moduledoc """ - Behaviour that can be used to write Tower reporters. + Behaviour that can be implemented to write Tower reporters. + + Tower comes built-in with a very naive `Tower.EphemeralReporter`, that implements + this behavior, which can be useful for development and testing purposes. """ @doc """ diff --git a/test/tower/ephemeral_reporter_test.exs b/test/tower/ephemeral_reporter_test.exs new file mode 100644 index 0000000..7a40bf7 --- /dev/null +++ b/test/tower/ephemeral_reporter_test.exs @@ -0,0 +1,4 @@ +defmodule Tower.EphemeralReporterTest do + use ExUnit.Case + doctest Tower.EphemeralReporter +end