`
hideto
  • 浏览: 2652334 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之exception_notification

    博客分类:
  • Ruby
阅读更多
每天一剂Rails良药之Getting Notified of Unhandled Exceptions中也提到了Exception Notification插件
该插件在production环境下,当程序出错时会给recipient发送notification邮件

Exception notification只会在访问的IP地址不为local时才工作,我们可以配置认为是local的IP:
consider_local "64.72.18.143", "14.17.21.25"
consider_local "64.72.18.143/24"


如果我们要清除本地地址列表,比如想让"127.0.0.1"不被认为是local的,我们可以在controller里加上:
local_addresses.clear


在views/exception_notifier目录下的视图文件可以访问如下变量:

* @controller: the controller that caused the error
* @request: the current request object
* @exception: the exception that was raised
* @host: the name of the host that made the request
* @backtrace: a sanitized version of the exception's backtrace
* @rails_root: a sanitized version of RAILS_ROOT
* @data: a hash of optional data values that were passed to the notifier
* @sections: the array of sections to include in the email


通过修改ExceptionNotifier.sections变量,我们可以添加和排除一些视图
添加视图时,我们只需把新的视图的名字加到ExceptionNotifier.sections列表并定义相应的partial即可
如果新定义的partial需要自定义的变量信息,我们可以使用exception_data宏:
class ApplicationController < ActionController::Base
  ...
  protected
    exception_data :additional_data

    def additional_data
      { :document => @document,
        :person => @person }
    end
  ...
end


默认下email notifier只会对critical errors进行notify
对于ActiveRecord::RecordNotFound和ActionController::UnknownAction只会render你的public/404.html文件
其他异常则会render你的public/500.html并发送email notification
具体规则参考exception_notification的源文件即可
如果你想更改发送email notification的规则,只需实现或修改rescue_action_in_public方法即可
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics