4

I am working on a rails app that has a WP home page, and also some images that are being load from WP. On localhost we don't have access to WP content that leads to having a lot of routing errors in logs, in example:

Started GET "/wp-content/uploads/2014/03/facebook-icon1.png" for 127.0.0.1 at 2015-11-20 15:10:48 +0200

ActionController::RoutingError (No route matches [GET] "/wp-content/uploads/2014/03/facebook-icon1.png"):
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

Considering we have 5 images on the page we end up having 5 routing errors for each request. How can I hide these type of errors from logs in dev environment?

rmagnum2002
  • 11,341
  • 8
  • 49
  • 86

2 Answers2

14

Had this exact problem. Create a logger.rb file in your initializers folder and add this code:

# spammers were blowing up our logs
# this suppresses routing errors
if Rails.env.production?
    class ActionDispatch::DebugExceptions
      alias_method :old_log_error, :log_error
      def log_error(env, wrapper)
        if wrapper.exception.is_a?  ActionController::RoutingError
          return
        else
          old_log_error env, wrapper
        end
      end
    end
end
Joshua
  • 5,336
  • 1
  • 28
  • 42
  • Remove the condition "if Rails.env.production?" and it will suppress Routing errors in all environments. – Joshua Nov 23 '15 at 18:24
  • I need this only for development, so I used `if Rails.env.development?`. Also, as you can see in gist I posted above it still generates logs, shorter than it was before I admit, but there are still some traces. Any chances to remove them at all.. to leave let's say only the requests.. Thank you. – rmagnum2002 Nov 24 '15 at 08:36
  • 1
    Yes, this code is easily adaptable to filter any log types out you want, or you could change it to only log certain types. If you take a moment to read through the code you can see where it decides to return without logging, or to go ahead and send info to the log. Change to suit. – Joshua Nov 24 '15 at 16:09
2

Maybe this silencer gem can help you.

Usage:

In your environment:

require 'silencer/logger'

config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :silence => [%r{^/wp-content/}]