第一次配置时,网上资料较乱,走了不少弯路。特在此记录。
配置环境:Ruby 1.9.3 Rails 3.2.2
1.安装 exception_notification gem
在Gemfile中添加
gem 'exception_notification', '3.0.0'
注:我第一次使用的是最新版本4.0.0,遇到一个错误 undefined method `underscore' for nil:nilclass 无法解决,后来换了3.0.0 就OK了
2.在environment.rb中配置
这是我的environment.rb, 仅供参考
其中HELLOWORLD为你自己的APP名称
# Load the rails application
require File.expand_path('../application', __FILE__) #这里配置 只有非开发环境才发送错误邮件
if Rails.env != "development"
HELLOWORLD::Application.config.middleware.use ExceptionNotifier,
:email_prefix => "这是邮件前缀",
:sender_address => %{"Warn!" 发信人邮箱@qq.com}, #收件人 可配置多个
:exception_recipients => ["123456@qq.com","654321@qq.com"],
:delivery_method => :smtp, #邮件服务器配置
:smtp_settings => {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => "plain",
:user_name => "XXXXX@gmail.com",
:password => "*********",
:enable_starttls_auto => true
}
end
# Initialize the rails application
HELLOWORLD::Application.initialize!
3.重启一下Rails程序 如果你是开发环境下测试,注意修改上面的配置
现在 如果程序出现异常 你应该会收到一封错误邮件 包含具体的报错信息
其他的具体使用方法:https://github.com/smartinez87/exception_notification