如何配置 Airbrake gem 以记录开发和生产环境中的所有 Rails 异常?

发布于 2024-12-06 18:45:56 字数 354 浏览 0 评论 0原文

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

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

目前我正在执行自述文件中列出的命令

script/rails generate airbrake --api-key your_key_here

I have found it difficult to send exceptions of my Rails 3 app via the Airbrake gem. At first I thought there was an Airbrake configuration error on my part, but after trial and error and reading the documentation (https://github.com/thoughtbot/airbrake#readme) very closely, I found out that Airbrake does not report errors when an app is running in the development environment. It does report errors when the app is running in the production environment.

Is there a flag to generate an Airbrake configuration file that automatically includes the development environment in the list of environments in which notifications should not be sent?

Currently I am executing the command listed in the README

script/rails generate airbrake --api-key your_key_here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南渊 2024-12-13 18:45:56

直截了当。

  config.consider_all_requests_local       = false

而不是

  config.consider_all_requests_local       = true

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

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

Straightforward.

  config.consider_all_requests_local       = false

instead of

  config.consider_all_requests_local       = true

in your config/environments/development.rb. In my case, like I suspect in many others, it was just a temporary change so I can "test" Airbrake's notify_airbrake.

You do need config.development_environments = [] in airbrake.rb

十雾 2024-12-13 18:45:56

不确定配置选项,但您可以使用从控制器显式向 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

Not sure about a config options, but you can explicitly send notifications to Airbrake from a controller using

notify_airbrake(exception)

So to do this in development, you could catch all errors in your application_controller, send a notification and then handle the errors as before. Take a look at rescue_from to get started. This is how I'm doing this to get notifications from my staging environment (or, to be exact, any environment other than development and test).

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文