【问题标题】:How do I configure the Airbrake gem to log all Rails exceptions in both development and production environments?如何配置 Airbrake gem 以记录开发和生产环境中的所有 Rails 异常?
【发布时间】:2011-11-25 14:30:34
【问题描述】:

我发现很难通过 Airbrake gem 发送我的 Rails 3 应用程序的异常。起初我以为我的 Airbrake 配置错误,但经过反复试验并仔细阅读文档(https://github.com/thoughtbot/airbrake#readme)后,我发现 Airbrake 不报告错误当应用程序在开发环境中运行时。当应用在生产环境中运行时,它确实会报告错误。

是否有生成 Airbrake 配置文件的标志,该文件自动将开发环境包含在不应发送通知的环境列表中?

目前我正在执行 README 中列出的命令

script/rails generate airbrake --api-key your_key_here

【问题讨论】:

    标签: ruby-on-rails exception error-logging hoptoad


    【解决方案1】:

    直截了当。

      config.consider_all_requests_local       = false
    

    而不是

      config.consider_all_requests_local       = true
    

    在您的config/environments/development.rb 中。就我而言,就像我在许多其他人中所怀疑的那样,这只是一个临时更改,因此我可以“测试” Airbrake 的 notify_airbrake

    airbrake.rb 中确实需要 config.development_environments = []

    【讨论】:

    • 我应该指出,除了你确实需要config.development_environments = [] in airbrake.rb
    • 另一件事是,要在 airbrake 中显示错误,它必须来自您在 airbrake 帐户设置中配置的子域(例如,可能不是 localhost)。您可以使用/etc/hosts 给自己一个合适的域(例如 dev.example.com)。除了其他两件事之外,还需要这样做。
    【解决方案2】:

    不确定配置选项,但您可以使用

    从控制器显式向 Airbrake 发送通知
    notify_airbrake(exception)
    

    因此,要在开发中执行此操作,您可以在 application_controller 中捕获所有错误,发送通知,然后像以前一样处理错误。看看rescue_from 开始吧。这就是我这样做是为了从我的暂存环境(或者,准确地说,是开发和测试以外的任何环境)获取通知的方式。

    class ApplicationController < ActionController::Base
    
      rescue_from Exception, :with => :render_error
    
      private
    
      def render_error(exception)
        render :file => "#{Rails.root}/public/500.html", :layout => false, :status => 500
        logger.error(exception)
        notify_airbrake(exception) unless Rails.env.development? || Rails.env.test?
      end
    end
    

    【讨论】:

    • 这是一个很好的起点。当我为 Exception 添加一个 rescue_from 方法时,我看到 500.html 文件已呈现并且日志已写入,如块的前两行中所指定。不幸的是,notify_airbrake 在开发或默认环境中都不起作用,只能在生产环境中使用(即使在我删除了除非条件之后)。
    • 我认为我理想的解决方案位于Airbrake::Configuration 类中,特别是development_environments 对象。 rdoc.info/github/thoughtbot/airbrake/master/Airbrake/…
    • 请看下面的补充答案!
    猜你喜欢
    • 2010-12-12
    • 2018-10-15
    • 2014-06-13
    • 2021-09-27
    • 2012-03-05
    • 2018-06-04
    • 2018-03-27
    • 1970-01-01
    • 2015-08-19
    相关资源
    最近更新 更多