jrochkind
jrochkind

Reputation: 23317

Customize rails uncaught exception logging

So if there's an uncaught exception in my Rails app, it gets logged, with a stacktrace.

Great. But I'd actually like to turn off "INFO" level logging, and only log WARN/ERROR/FATAL.

Which means, for the uncaught excpetions, I REALLY want it to log a lot more about the current request, not just an exception name and stack trace. I want the request params, the request URI, I even want the request client IP and user-agent.

I am having trouble finding what part of Rails to customize to get this. Whether by config alone or by over-riding a method, or even by monkey patching -- I can't quite figure out where this actually happens.

Is that because it's off in middleware? Bah! Either way... any hints as to the easiest way to actually do this?

(It is surprising that Rails does not make this easy, no? It seems like not that unusual of a thing? Is it because everyone that cares is using some third party platform for catching these things instead of log files? I'm not, heh.)

Upvotes: 3

Views: 1136

Answers (2)

moiristo
moiristo

Reputation: 46

See https://github.com/rails/rails/issues/9343, this is not supported yet. It seems that the DebugExceptions middleware is logging this. The only solution I see atm is to swap out this middleware using config.middleware.swap and replacing it with a custom version.

Upvotes: 3

matt.early
matt.early

Reputation: 225

You can add additional exception handling by catching all exceptions with rescue_from.

For example, you can add this to your application_controller.rb:

rescue_from Exception, :with => :internal_error

def internal_error(e)
   logger.error request.fullpath
   raise e
end

By re-raising the original exception you ensure rails gets to see it also.

Upvotes: 8

Related Questions