原创作者: hideto
阅读:917次
评论:0条
更新时间:2011-05-26
每天一剂Rails良药之Getting Notified of Unhandled Exceptions中也提到了Exception Notification插件
该插件在production环境下,当程序出错时会给recipient发送notification邮件
Exception notification只会在访问的IP地址不为local时才工作,我们可以配置认为是local的IP:
如果我们要清除本地地址列表,比如想让"127.0.0.1"不被认为是local的,我们可以在controller里加上:
在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宏:
默认下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方法即可
该插件在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方法即可
评论 共 0 条 请登录后发表评论