【发布时间】:2014-06-27 14:17:01
【问题描述】:
我想要做的是无错误地输出以记录所有基本上会导致 404 的请求?还是单独记录此类请求?
【问题讨论】:
标签: ruby-on-rails logging ruby-on-rails-4 request
我想要做的是无错误地输出以记录所有基本上会导致 404 的请求?还是单独记录此类请求?
【问题讨论】:
标签: ruby-on-rails logging ruby-on-rails-4 request
您最好挂接到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
我不确定你如何使它成为有条件的(即只捕获“未找到”异常),但上面肯定是一个起点!
【讨论】:
如果您的 404 是由于丢失导致的,我建议使用 rescue_from,您的应用程序控制器中的代码将类似于:
rescue_from ActiveRecord::RecordNotFound, with: :not_found
def not_found
# log anything in the request
end
【讨论】: