1

有没有办法将 Airbrake 与纯 Ruby 项目(不是 rails 或 sinatra)集成,以便报告意外错误?我已经设置好了,我可以通过调用Airbrake.notify_or_ignore和传入异常来捕获错误,但是如果不显式调用它,我无法让它报告错误。

以下是适用于显式调用Airbrake.notify但不适用于在未显式调用的情况下向 Airbrake 发送错误的代码notify

require 'airbrake'

Airbrake.configure do |config|
  config.api_key = ENV['AIRBRAKE_API_KEY']
  config.development_environments = []
  config.ignore_only = []
end

我尝试使用以下代码将 Rack 添加为中间件:

require 'rack'
require 'airbrake'


Airbrake.configure do |config|
  config.api_key = ENV['AIRBRAKE_API_KEY']
  config.development_environments = []
  config.ignore_only = []
end


app = Rack::Builder.app do
    run lambda { |env| raise "Rack down" }
end

use Airbrake::Rack
run app

但我得到一个“未定义的方法‘使用’用于 main:Object (NoMethodError)”

有什么想法吗?

4

1 回答 1

-1

从马克的评论链接复制到未来谷歌员工的空气制动:

# code at http://gist.github.com/3350
# tests at http://gist.github.com/3354

class Airbrake < ActiveResource::Base
  self.site = "http://your_account.airbrake.io"

  class << self
    @@auth_token = 'your_auth_token'

    def find(*arguments)
      arguments = append_auth_token_to_params(*arguments)
      super(*arguments)
    end

    def append_auth_token_to_params(*arguments)
      opts = arguments.last.is_a?(Hash) ? arguments.pop : {}
      opts = opts.has_key?(:params) ? opts : opts.merge(:params => {}) 
      opts[:params] = opts[:params].merge(:auth_token => @@auth_token)
      arguments << opts
      arguments
    end
  end
end

class Error < Airbrake  
end

# Errors are paginated. You get 30 at a time.

@errors = Error.find :all
@errors = Error.find :all, :params => { :page => 2 }
于 2015-04-22T01:20:28.887 回答