【问题标题】:Rails - How to change log level for ActionController::RoutingErrorRails - 如何更改 ActionController::RoutingError 的日志级别
【发布时间】:2012-02-02 07:26:01
【问题描述】:

有没有办法更改ActionController::RoutingError 的日志级别?
我想将其从错误更改为警告。谷歌搜索并没有找到解决方案...
我只想将路由错误显示为警告。但不会更改其他日志记录行为。
此处描述了一种可能的解决方法(或该解决方法的一部分)https://stackoverflow.com/a/5386515/10272,但这并不是我想要的。在这个解决方案中,他们只是为 page-not-found 错误添加了一个 catch-all 路由器,然后在 catch-all-controller 中处理它。但我希望能够仅更改日志严重性,如果可能的话,它似乎更好地描述了我的意图......

【问题讨论】:

    标签: ruby-on-rails logging


    【解决方案1】:

    我在 Rails 5.2 中使用初始化程序解决了这个问题:

    # /config/initializers/routing_error_logger_defatalizer.rb
    
    # Spammers and script kiddies hit the site looking for exploits
    # which blows up our logs as we get a bunch of fatal errors for 404s on eg. "/wp-admin.php"
    # This initializer changes the log level of RoutingErrors to 'warn' (so they still appear in logs but are not fatal)
    # this means we can use logging notifications on eg. >= error without all the false positives
    # ref: https://stackoverflow.com/questions/33827663/hide-actioncontrollerroutingerror-in-logs-for-assets
    # ref: https://github.com/roidrage/lograge/issues/146#issuecomment-461632965
    if Rails.env.production?
      module ActionDispatch
        class DebugExceptions
          alias_method :old_log_error, :log_error
          def log_error(request, wrapper)
            if wrapper.exception.is_a?  ActionController::RoutingError
              # Have prepended "[404]" to the message for easier log searches, otherwise it is the default Rails messaging
              # Full message eg. "[404] ActionController::RoutingError (No route matches [GET] \"/wp-login.php\")"
              logger(request).send(:warn, "[404] ActionController::RoutingError (#{wrapper.exception.message})")
              return
            else
              old_log_error request, wrapper
            end
          end
        end
      end
    end
    

    对于 Rails 4.2 使用:

    if Rails.env.production?
      class ActionDispatch::DebugExceptions # Rails 4.2
        alias_method :old_log_error, :log_error
        def log_error(request, wrapper)
          if wrapper.exception.is_a?  ActionController::RoutingError
            # Have prepended "[404]" to the message for easier log searches, otherwise it is the default Rails messaging
            # Full message eg. "[404] ActionController::RoutingError (No route matches [GET] \"/wp-login.php\")"
            logger(request).send(:warn, "[404] ActionController::RoutingError (#{wrapper.exception.message})")
            return
          else
            old_log_error request, wrapper
          end
        end
      end
    end
    

    【讨论】:

    • 我也很欣赏这些有趣的 cmets :) wp-admin.php 整天
    【解决方案2】:

    您可以覆盖Simple Formatter,以便将错误记录为警告。

    但是,我不建议采用这种方式,因为您最终会为应用程序视为错误/异常的操作记录警告消息。更优雅的选择是拥有一个如您所描述的包罗万象的页面,控制器捕获异常记录警告消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-17
      • 2016-02-22
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多