Hey ,
I want to set up exception logging in a Hanami 2.2 app (api-only). If you do not do this and use the :json
formatter in the production
environment, then the log file contains a mixture of json
lines and string
lines. Furthermore, by default, the exceptions are swallowed up in the productive env, because of the RenderErrors
-middleware.
How I did it:
By default (config.render_errors = true
), the exception is swallowed and not logged at all. Error html pages are rendered, which is not intended with api only.
With the following config I changed this behavior:
# config/app.rb
require "hanami"
module MyApp
class App < Hanami::App
#...
environment(:production) do
config.render_errors = false #
config.logger.formatter = :json
end
end
end
Then I set up exception logging:
# app/action.rb
# auto_register: false
# frozen_string_literal: true
require "hanami/action"
require "dry/monads"
module MyApp
class Action < Hanami::Action
# Provide `Success` and `Failure` for pattern matching on operation results
include Dry::Monads[:result]
handle_exception Exception => :handle_base_exception
private
def handle_base_exception(_, _, exception)
Hanami.app["logger"].error(exception)
end
end
end
However, I believe that with this solution, exceptions that occur in the json middleware, for example, are not logged correctly. What is your opinion, or how would you proceed? Is there a better way?
Should we add Hanami.app["logger"].error(exception)
to the RenderErrors
-middlware?
Thanks