Hide ActionController::RoutingError in logs for assets

柔情痞子 提交于 2021-01-27 07:13:04

问题


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?


回答1:


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



回答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/}]


来源:https://stackoverflow.com/questions/33827663/hide-actioncontrollerroutingerror-in-logs-for-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!