views:

101

answers:

3

What's the best way to send quick one-liner e-mails to an administrative address in Rails?

I am using the Rails Logger to keep track of nasty problems in my application. One of my controllers is doing mission-critical work and I need to know of any errors ASAP. When there is an error, I would like my app to send an email to myself, calling attention to the log containing more details. I'm envisioning something like the following pseudo-code:

begin
   ...
rescue
   logger.error("Found an error!  Params hash is " + params.to_s)
   email("[email protected]","Critical error in log.  Investigate line " + len(logger.error).to_s)
end

The obvious first place I looked at was ActionMailer. However, both the documentation and my literature talk about creating a mailer Model, View & Controller, which seems overly complex for what I want: A very short (one-line) e-mail to an email address where formatting & reply-to address doesn't matter at all. This brings me back to my question: What's the best way to send quick one-liner e-mails to an administrative e-mail address in Rails?

A: 

There are plenty of exception notification plugins for Rails: http://www.ruby-toolbox.com/categories/exception_notification.html

hgimenez
http://github.com/rails/exception_notification is crazy simple to set up.
Jonathan Julian
+3  A: 

Take a look at the Pony gem (as in Pony Express), which is a lightweight alternative to ActionMailer:

require 'pony'
Pony.mail(:to => '[email protected]', :from => '[email protected]',
          :subject => 'Critical error in log')

Alternatively, you could sign up for a free account with Hoptoad and raise an exception from your application. They'll capture it and send you an email.

John Topley
Voting up for the Hoptoad suggestion.
Mike Buckbee
A: 

As a general practice, I would prefer (external) centralized logging server and avoid modifying "mission critical" application just to send out error notifications. Sorry, no familiar with Rails, but in case there is a port of log4j for Rails, I'd recommend looking at logFaces which has sophisticated mechanism of trapping log statements and sending out e-mail notifications. It's commercial product though. I believe there are other solutions as well. In any case, it's always safer to use an external module than fiddling with the existing code base, who knows what e-mail sending will do to a mission critical program, unless its sole mission is to send emails of course :)

Dima