Keep 404's out of my logs

So every time something requests something from my rails (2.x) app that
doesn’t actually map to a controller, I wind up with an error in my
production.log. ActionController::RoutingError or
ActionController::UnknownAction depending on the nature of the bad
request.

Is there any easy way to tell Rails to keep these out of my logs? Or
put them in a different log? Or at least omit the backtrace (which is
not a useful backtrace, since it’s to Rails internals and is always the
same for an UnknownAction or RoutingError).

By the time rescue_action_in_public is called, it seems to be too late,
the thing has already been logged.

Thanks for any ideas!

Jonathan R. wrote:

Is there any easy way to tell Rails to keep [RoutingError, UnknownAction, UnknownController] out of my logs? Or
put them in a different log? Or at least omit the backtrace (which is
not a useful backtrace, since it’s to Rails internals and is always the
same for an UnknownAction or RoutingError).

By the time rescue_action_in_public is called, it seems to be too late,
the thing has already been logged.

Figured it out myself looking at source in ActionController::rescue.rb .
I’ll put it here for the archives.

In Rails 2.1 (and hopefully 2.x in general), rescue_action_in_public is
called by rescue_action.

rescue_action also calls log_error, after calling
rescue_action_in_public.

It’s log_error that writes uncaught exceptions to the log as fatal, with
complete backtrace.

So I can override log_error, and check the class of the error. If it’s
not a routing type error, call super. If it is, I can not log it, log it
to a different logger, log it with a different severity, whatever.

Or you could just define a catch-all route as your last defined route,
and do something like the following:

last route defined in routes.rb:


map.connect
‘*path_leftovers’, :controller=>‘testapp’, :action=>‘pre_404’

in testapp_controller.rb or application(_controller).rb:


def pre_404
logger.warn(“WARN: pre_404: path=#{request.path}”)
redirect_to ‘/404.html’
end

Or you could define another cath-all route per controller, or you
could dev your own custom 404-like meth/screen, or set some error msg
for display in and redirect to some default action (say index) for the
given controller, or … It all depends on what you want to do.

Note that params[:path_leftovers] in pre_404 would be an Array of
String path-chunks.

Jeff

On Apr 16, 1:15 pm, Jonathan R. <rails-mailing-l…@andreas-

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs