异常通知Gem与Rails 3

41

我在尝试运行这个程序,但每当我启动服务器时都会看到 "未初始化的常量ExceptionNotifier"。

http://github.com/rails/exception_notification

在我的Gemfile中,我有

gem "exception_notification",:git => "http://github.com/rails/exception_notification.git", :branch => "master"

我尝试将Github readme中显示的配置放置在config/application.rb、config/environment.rb和config.ru中。我用我的应用程序名称替换了“Whatever”。


请查看Sebastian Martinez下面的评论。他维护了官方插件,现在已经移到了新的位置(https://github.com/smartinez87/exception_notification)。它在Rails 3中运行无误。 - gtd
17个回答

57

所有之前的答案都已经过时了,现在你可以简单地将这个添加到你的gemfile中:

gem 'exception_notification', :require => 'exception_notifier'

按照readme中的指示编辑您的production.rb配置文件:

config.middleware.use ExceptionNotifier,
  :email_prefix => "[Exception] ",
  :sender_address => %{"Exception Notifier" <support@example.com>},
  :exception_recipients => %w{you@me.com}

7
我认为这是主项目的一个分支,你可以直接使用gem 'exception_notification',并加上:require => 'exception_notifier'。 - Braden Becker
3
布雷登是正确的 - gem 'exception_notification',:require => 'exception_notifier' 现在可用。 - William Denniss

21

13

好的,对我来说现在已经可以工作了:

# Gemfile
gem "exception_notification", :git => "git://github.com/rails/exception_notification", :require => 'exception_notifier'

# application.rb, inside the config block
config.middleware.use ::ExceptionNotifier,
  :email_prefix => "ApplicationName-Errors: ",
  :sender_address => %w{office@application.com},
  :exception_recipients => %w{office@application.com}

10

保持简单傻瓜

gemfile

gem 'exception_notification', :require => 'exception_notifier'

application.rb 文件

  config.middleware.use ExceptionNotifier,
 :email_prefix => "[ERROR] ",
 :sender_address => %{"Exception Notifier" <Dummy_email@exapmle.com>},
 :exception_recipients => %w{Dummy_email@example.com}
你已经完成了... :*

当然可以,但这会捕获所有的异常。它不会渲染任何内容。 - PabloJimeno

4

官方github仓库现在是: https://github.com/smartinez87/exception_notification

Gemfile中:

gem "exception_notification", :require => 'exception_notifier', :git => "https://github.com/smartinez87/exception_notification.git"

在 config\initializers\exception_notification.rb 文件中。
Rails.application.config.middleware.use ExceptionNotifier,
  :email_prefix => "[Whatever] ",
  :sender_address => %{"notifier" <notifier@example.com>},
  :exception_recipients => %w{exceptions@example.com}  

4
似乎Rails 3无法使用这个插件的gem形式。也许rack应用程序无法从gems中加载?我改为将其安装为插件,并将配置语法更改为:
config.middleware.use "::ExceptionNotifier"
而不是
config.middleware.use ExceptionNotifier

3

现在,这个过程变得更加简单了。您只需要在Gemfile中编写以下内容:

gem "exception_notification", :git => "http://github.com/rails/exception_notification.git", :require => 'exception_notifier'

所有问题都应该已经解决了。我认为:require选项非常关键(因为名称不同,你必须明确指定)。我想之前提到的所有其他补丁都已经合并了。


是的,绝对没问题。我在我的Gemfile中使用了上面的代码行,在我的application.rb文件中添加了config.middleware.use ...,这就是我需要做的全部。你还是遇到了未初始化常量错误吗? - nathanvda
:git 部分也是至关重要的。最新发布的 gem(目前为 2.3.3.0)与 Rails 3 不兼容。 - Steve Madsen

3

我能够在production.rb文件中使用以下内容使其正常工作:

config.after_initialize do
 config.middleware.use ExceptionNotifier,
      :email_prefix => "[Whatever] ",
      :sender_address => %{"notifier" <notifier@example.com>},
      :exception_recipients => %w{exceptions@example.com}
end

3
如果您正在使用rescue_from Exception, with: :render_500来处理显示500模板/页面,则不再仅发送电子邮件。
    config.middleware.use ExceptionNotifier,
  :email_prefix => "[some prefix] ",
  :sender_address => %{"Notifier" <notify@domain.com>},
  :exception_recipients => %w{recipient@domain.com}

你需要在处理异常的方法中手动发送它。
def render_500(exception)
    # email an error email if there's a 500 in production mode
    if Rails.env.production?
        ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
    end
end 

将配置文件内容放在环境变量中(production.rb),并将异常处理代码放在应用控制器中。

3

https://github.com/smartinez87/exception_notification

这个宝石已经更新到适用于Rails 3.x,我刚在3.0.7上测试了一下,安装过程变得更加简单。

Gemfile:

gem 'exception_notification'

初始化器:

Rails.application.config.middleware.use ExceptionNotifier,
  :email_prefix => "[Whatever] ",
  :sender_address => %{"notifier" <notifier@example.com>},
  :exception_recipients => %w{exceptions@example.com}

初始化器?这是哪个文件/在哪里? - sscirrus
1
@sscirrus 只需在 config/initializers/ 中创建一个任意命名的文件即可。 - gtd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接