如何在生产模式下运行rails应用程序时启用错误报告?
问题描述:
我正在使用Rails应用程序。 只要出现错误,我就可以在我的生产服务器中看到下面的屏幕。 (文字:很抱歉,但出错了)如何在生产模式下运行rails应用程序时启用错误报告?
如何在我的服务器中启用错误报告以查看实际错误? 我是否必须在Rails应用程序中进行任何配置更改或在我的服务器中启用错误报告。
注:我的服务器是Ngnix。
答
如果您添加到您的config/environments/production.rb
:
config.consider_all_requests_local = true
你会看到同样的错误,在开发中,但它也将禁用缓存为生产应用等
我宁愿建议你看看/log/production.log
。通常在屏幕上显示相同的错误。
答
对于错误报告,有一个称为exception_notification的方便宝石,它在您的应用程序中每次发生异常时都会向您发送邮件。
如果您还想在页面上获得详细且完全可自定义的错误报告,则必须进行一点编码。 下面是我用什么(我愿意给你一个链接,我已经采取了这种来自哪里,但我不能再找到它:()
-
定义一个
ErrorsController
这样的:class ErrorsController < ApplicationController ERRORS = [ :internal_server_error, :not_found, :unprocessable_entity, :unauthorized ].freeze ERRORS.each do |e| define_method e do respond_to do |format| format.html { render e, :status => e } format.any { head e } end end end
端
-
创建初始化到控制器挂钩到滑轨异常处理链:
require 'action_dispatch/middleware/show_exceptions' module ActionDispatch class ShowExceptions private def render_exception_with_template(env, exception) env['exception'] = exception body = ErrorsController.action(rescue_responses[exception.class.name]).call(env) log_error(exception) env.delete 'exception' body rescue Exception => e log_error(e) render_exception_without_template(env, exception) end alias_method_chain :render_exception, :template end end
为
ErrorsController::ERRORS
中的每个错误创建视图(即, app/views/errors/not_found.html.erb等)。
我不主张这是最好的技术,但它使我受益匪浅到目前为止(你可以轻松地添加页新的错误,定制想要的方式和内容单独显示每个错误)。