【问题标题】:Rails 4 log all not found requests to the applicationRails 4 将所有未找到的请求记录到应用程序中
【发布时间】:2014-06-27 14:17:01
【问题描述】:

我想要做的是无错误地输出以记录所有基本上会导致 404 的请求?还是单独记录此类请求?

【问题讨论】:

    标签: ruby-on-rails logging ruby-on-rails-4 request


    【解决方案1】:

    您最好挂接到config.exceptions_app 中间件挂钩:

    设置由 ShowException 调用的异常应用程序 发生异常时的中间件。默认为 ActionDispatch::PublicExceptions.new(Rails.public_path)。

    --

    exceptions_app

    拥有written a gem about this,并且有一些真正的nice demonstrations 可以有效地使用它,我建议您这样做:

    #config/application.rb
    config.exceptions_app = ->(env) { ApplicationController.action(:exception).call(env) }
    
    #app/controllers/application_controller.rb
    Class ApplicationController < ActionController::Base
       def exception
           # your code here
       end
    end 
    

    我不确定你如何使它成为有条件的(即只捕获“未找到”异常),但上面肯定是一个起点!

    【讨论】:

      【解决方案2】:

      如果您的 404 是由于丢失导致的,我建议使用 rescue_from,您的应用程序控制器中的代码将类似于:

      rescue_from ActiveRecord::RecordNotFound, with: :not_found
      
      def not_found
       # log anything in the request
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2018-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多