2013-03-21

Rails の Exception Notification で例外を Gmail 通知させてみた

Ruby on Rails のアプリで例外が発生したらメールでお知らせを受け取りたい。exception_notification という gem を使うと可能になるというので、設定してみた。

exception_notification の設定

Gem install

Gemfile に次の一行を追記して、bundle install を実行する。

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

作者の Sebastian Martinez 氏から、最新版の exception_notification なら「:require => 'exception_notifier'」の記述はいらないよ、とコメントをもらった [Thank you, Sebastian]。

gem 'exception_notification'

メールを送付するための Action Mailer の gem も自動でインストールされるので助かる。

設定

config/environments/development.rb にメール送付の設定を書く。上手く行ったら、config/environments/production.rb にコピー。

  # Exception Notification
  config.middleware.use ExceptionNotifier,
    :email_prefix => "[プロジェクト名] ",
    :sender_address => %{"notifier" <notifier@example.com>},
    :exception_recipients => %w{exceptions@example.com}

エラーが出たら、メールを送りたい。でも、とりあえずメール設定は後回しにしたいので、ターミナルに出力させてみる。次の一行を config/environments/development.rb に追記。

  config.action_mailer.delivery_method = :test
例外のコード

手元に例外を出すコードがなかったので、ちょっと強引に行く。Ruby on Rails Guides のブログ・アプリで新規作成ページに行ったら例外を出す、本当に強引なコード。

app/controllers/posts_controller.rbdef new に次の様に例外発生コードを挿入。

  def new
    raise "exception sample"
...
例外を出す

さて、ブラウザーを起動して「新規作成」ページへ。例外が発生するはず。xterm にはこんな文章が出てくれば OK。

Sent mail to exceptions@example.com (7ms)
Date: Thu, 21 Mar 2013 11:59:53 +0900
From: notifier <notifier@example.com>
To: exceptions@example.com
Message-ID: <514a77a971208_16764fb37dc647a@localhost.mail>
Subject: [Try-RoR] posts#new (RuntimeError) "exception sample"
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

A RuntimeError occurred in posts#new:

  exception sample
  app/controllers/posts_controller.rb:31:in `new'

...

Gmail 設定

Action Mailer の設定を config/environments/development.rb もしくは config/environments/production.rb に書く。先に書いた terminal に例外情報を出力するコードはコメント・アウトする。

  # Action Mailer for Gmail
  #  config.action_mailer.delivery_method = :test
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => 'smtp.gmail.com',
    :port => 587,
    :domain => 'gmail.com',
    :user_name => 'your_account@gmail.com',
    :password => 'your_password',
    :enable_starttls_auto => true
  }

これで例外を発生させて Gmail からメールが届けば成功。

わざと例外を発生させてる人は、そのコードを削除してお終い。

あとがき

Rails2 時代と比べて、SSL 関連の設定が楽になってて幸せだった。Action Mailer の SMTP を Gmail ではなく Yahoo! Mail (yahoo.com の方) を使いたかったけど、上手くいかなかった。同じ様な設定で行けるはずなんだけど...

そうそう、自分のサーバーからメールを送る時は postfix あたりを使うと良いらしい。暇が出来たら試してみたいね。

$ sudo apt-get install postfix
config.action_mailer.delivery_method = :sendmail

ref

3 件のコメント:

  1. Note that the official repo lives now on https://github.com/smartinez87/exception_notification.

    Also, with the latest version you don't need the `require` on this line `gem 'exception_notification', :require => 'exception_notifier'` anymore.

    返信削除
    返信
    1. Thank you for your comment, and reading my blog entry.
      I'll note your comment in this entry.
      Thanks again.

      削除
  2. このコメントは投稿者によって削除されました。

    返信削除