Receive errors with Telemetry

Hello, I am trying to learn more about Telemetry events in Elixir and to do that I am building an Elixir “error monitoring” package.

I currently am able to receive some Phoenix errors with the following telemetry event handler:

   # application.ex
   :telemetry.attach("phoenix-stop-handler", [:phoenix, :endpoint, :stop], &Foobar.TelemetryHandler.handle_event/4, nil)
   
   # Foobar.TelemetryHandler
   def handle_event([:phoenix, :endpoint, :stop], measurements, metadata, _config) do
     case metadata[:reason] do
       {:error, reason} ->
         Foobar.capture_error(reason, metadata)
       _ ->
         :ok
     end
   end

However, I’m wondering if it’s possible to easily tie into all Elixir application errors? I envision something like this, but not sure if this is possible:

   def handle_event([:any, :application, :error], measurements, metadata, _config) do
     case metadata[:reason] do
       {:error, reason} ->
         Foobar.capture_error(reason, metadata)
       _ ->
         :ok
     end
   end

I think you might be able to do that with a custom logger backend: Logger — Logger v1.12.3

I hadn’t considered this and will look into it. Thank you.