Forum: Ruby on Rails keep 404's out of my logs

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2009-04-16 21:13
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!
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2009-04-16 22:15
Jonathan Rochkind 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.
0d56133042e4814c1b2255ea655f2361?d=identicon&s=25 Jeff Burlysystems (jburly)
on 2009-04-16 22:39
(Received via mailing list)
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 Rochkind <rails-mailing-l...@andreas-
This topic is locked and can not be replied to.