# Keep 404's out of my logs

**URL:** <https://www.ruby-forum.com/t/keep-404s-out-of-my-logs/165736>\
**Category:** Rails\
**Created:** [April 16, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/keep-404s-out-of-my-logs/165736 "2009-04-16T21:13:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 16, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/keep-404s-out-of-my-logs/165736/1 "2009-04-16T21:13:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 16, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/keep-404s-out-of-my-logs/165736/2 "2009-04-16T22:15:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 16, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/keep-404s-out-of-my-logs/165736/3 "2009-04-16T22:39:27Z")

</div>

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-
