[url=http://hideto.iteye.com/blog/81144]每天一剂Rails良药之Getting Notified of Unhandled Exceptions[/url]中也提到了Exception Notification插件
该插件在production环境下,当程序出错时会给recipient发送notification邮件
Exception notification只会在访问的IP地址不为local时才工作,我们可以配置认为是local的IP:
[code]
consider_local "64.72.18.143", "14.17.21.25"
consider_local "64.72.18.143/24"
[/code]
如果我们要清除本地地址列表,比如想让"127.0.0.1"不被认为是local的,我们可以在controller里加上:
[code]
local_addresses.clear
[/code]
在views/exception_notifier目录下的视图文件可以访问如下变量:
[b]
* @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
[/b]
通过修改ExceptionNotifier.sections变量,我们可以添加和排除一些视图
添加视图时,我们只需把新的视图的名字加到ExceptionNotifier.sections列表并定义相应的partial即可
如果新定义的partial需要自定义的变量信息,我们可以使用exception_data宏:
[code]
class ApplicationController < ActionController::Base
...
protected
exception_data :additional_data
def additional_data
{ :document => @document,
:person => @person }
end
...
end
[/code]
默认下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:
[code]
consider_local "64.72.18.143", "14.17.21.25"
consider_local "64.72.18.143/24"
[/code]
如果我们要清除本地地址列表,比如想让"127.0.0.1"不被认为是local的,我们可以在controller里加上:
[code]
local_addresses.clear
[/code]
在views/exception_notifier目录下的视图文件可以访问如下变量:
[b]
* @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
[/b]
通过修改ExceptionNotifier.sections变量,我们可以添加和排除一些视图
添加视图时,我们只需把新的视图的名字加到ExceptionNotifier.sections列表并定义相应的partial即可
如果新定义的partial需要自定义的变量信息,我们可以使用exception_data宏:
[code]
class ApplicationController < ActionController::Base
...
protected
exception_data :additional_data
def additional_data
{ :document => @document,
:person => @person }
end
...
end
[/code]
默认下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方法即可
本文介绍了Rails中的ExceptionNotification插件使用方法。该插件能在生产环境中遇到未处理的异常时,向指定收件人发送邮件通知。文章还详细解释了如何配置插件来决定哪些错误需要发送通知,以及如何自定义邮件中的错误信息。

1304

被折叠的 条评论
为什么被折叠?



