#104
May 05, 2008

Exception Notifications

If you're running a production site, it's a good idea to record all exceptions which occur. See how to set up a notification system which does that in this episode.
Download (18.6 MB, 8:58)
alternative download for iPod & Apple TV (12.2 MB, 8:58)

Links

Note: I forgot to mention, the exception_logger plugin requires will_paginate, so make sure you have that installed either through a plugin or gem.

Also I should have mentioned the importance of adding authorization to the controller so not everyone can access it. The README shows you how to do this.

gem install will_paginate
git clone git://github.com/ryanb/exception_logger.git vendor/plugins/exception_logger
script/generate exception_migration
rake db:migrate
# config/environments/development.rb
config.action_controller.consider_all_requests_local = false

# in controllers/application.rb
include ExceptionLoggable

private

def local_request?
  false
end

# routes.rb
map.connect "logged_exceptions/:action/:id", :controller => "logged_exceptions"

RSS Feed for Episode Comments 29 comments

1. Olli May 05, 2008 at 03:03

Maybe its a cool idea to use jabber (aka xmpp) for exeption notification to the admin.


2. Collin VanDyck May 05, 2008 at 05:39

I always look forward to seeing a new episode in my Railscasts feed. It's very helpful to know how to simulate the production error environment, and the reporting plugin looks really nice. Keep up the good work!


3. Russ Jones May 05, 2008 at 09:05

Doesn't appear to work with Fast Sessions. http://code.google.com/p/rails-fast-sessions/ No matter what I do I get an error complaining ... "No :secret given to the #protect_from_forgery call" on line #53 of logged_exceptions/index.rhtml ... even though I do have one specified. The errors are logged, but the logged_exception views wont show.


4. Russ Jones May 05, 2008 at 09:54

Not to sound ungrateful! Love your stuff of course! I'm on the edge of my seat with each episode. You're one of the reasons I love R(uby|ails)!


5. Jeremy May 05, 2008 at 12:22

Hi

all the same as Russ Jones : the problem with the :secret and the love of your work !

I don't use Rails Fast Sessions, but I disabled session store in cookies. Maybe it's something like that.


6. Ryan Bates May 05, 2008 at 12:46

@Russ & Jeremy, oh interesting, I hadn't tried this without using cookie session store. It may have something to do with the fact that LoggedExceptionsController doesn't inherit from ApplicationController. You may need to add some specific session info to the controller, check out the readme for an example. I'll do some testing and post what I find.

In many ways I wish this plugin was just a generator which created the controller instead of keeping it buried in a place where it's difficult to change. This would solve these kinds of problems.


7. Clemens Kofler May 05, 2008 at 13:09

Nice episode!

If - for whichever reason - you don't want to use one of the plugins, there's another possibility that I'm currently using in one of my projects: I symlinked the production.log in the public directory (of course it's password protected) so I can check the log at any time and don't have to log in at the server.


8. QuBiT May 06, 2008 at 06:02

Nice one, really helpful!

Got it running ^^, just quick and dirty.

for those who had problems with the "secret"

just use:
protect_from_forgery :secret => 'mytinylittleveryultramegalongsecret'

with your secret in the logged_exceptions_controller.rb

and for me i had to use:
helper_method :filtered? and copy that method from the helper file (logged_exceptions_helper) to the controller file in the plugin/lib, because it looks like that the helper is not loading correct.

would be nice, if the logging process itself would be written to the log file ("INSERT INTO ...") for better documentation ... etc etc


9. Heiko May 06, 2008 at 07:08

What I do for security reasons is putting any admin or reporting pages into a separate application, as described here:

http://www.rorsecurity.info/2008/03/03/intranet-and-admin-security/

This is deescalation, a stolen cookie/session/account means less harm.


10. Mike Boone May 06, 2008 at 14:31

Does anyone know what's to become of the code on dev.rubyonrails.org that didn't get migrated to GitHub? Stuff like plugins/exception_notification and adapters/oracle.

Thanks for the Railscasts!


11. Antonio Reina May 07, 2008 at 11:55

Hi

I think that in #config/environments/production.rb code to require authentication, you need add after: accepts.html { login_required_without_basic }
other line for rjs format:
accepts.js { login_required_without_basic }

Moreover, the plugin requires RedCloth gem for exceptions details.


12. Andreas Neuhaus May 07, 2008 at 13:05

I didn't try exception_logger yet, but it looks promising. I had a quick look to the source and saw, that it provides an RSS feed of logged exceptions - so you can use a feed reader to subscribe to the exception logs of your application.


13. Scott Tamosunas May 07, 2008 at 21:06

Thanks Ryan for the excellent railscast, as always.

I am getting an exception when I try to protect the logged_exceptions behind an admin authentication (I am using restful_authentication as well):

/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in `const_missing': uninitialized constant Rails::Initializer::AuthenticationSystem (NameError)


14. Aleksandr May 11, 2008 at 14:34

When I try to download the "exception_logger Plugin [ryanb fork]" from GitHub - the archive doesn't have all the files.


15. Andres Rodriguez May 14, 2008 at 22:00

Hi, Im getting this error when running script/generate exception_migration

Does anyone have an idea of what's going on?

/vendor/plugins/exception_logger/init.rb:3:in `load_plugin': undefined method `view_paths=' for LoggedExceptionsController:Class (NoMethodError)


16. Lawrence Pit May 18, 2008 at 23:00

Having trouble adding authentication to the exception_logger controller? See:
 http://blog.lawrencepit.com/2008/05/07/recording-rails-exceptions/


17. Rob May 21, 2008 at 03:41

All installed great, no probs there - but it doesn't log 404 errors. Do you need to hook something in so the exception logger knows about them?


18. Federico Jun 04, 2008 at 10:00

Same problem as Rob, it's not logging any errors.


19. Vincent Jun 09, 2008 at 13:31

For Windows Users:
Download and install Git program.

http://code.google.com/p/msysgit/

Then follow run commands above.


20. AJ Morris Jun 24, 2008 at 11:26

I'm trying to get this to work with Rails 2.1 and when run it locally following your instructions here, it works beautifully. However when I get it up on the server and try to start my mongrels I get 1/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:492:in `const_missing': uninitialized constant ApplicationController::ExceptionLoggable (NameError).

I'm not sure why it's not working. In my production.rb file I put what the README said, but it doesn't seem to find the EsceptionLoggable. Is there any more gem requirements besides will_paginate?


21. Eric Berry Jun 24, 2008 at 12:17

I had a lot of trouble getting this working as well. To have a bit more control over what was being logged, I merged the model, helper and controller from the plugin into my actual app. I placed the controller behind my admin auth so it inherited the security settings automatically. I also disregarded the exception_loggable.rb file and added a protected method in the application.rb file. Here's how it looks:

  protected

   def log_error(exception)
     super(exception)
     begin
       LoggedException.create_from_exception(controller_name, action_name, request, exception)
     rescue => e
       puts "EXCEPTION: #{e.message}"
       logger.error(e)
     end
   end

I am sure this is not the cleanest way to do it, but it does cut a lot of the fat away from the meat.


22. Kiere El-Shafie Jun 27, 2008 at 22:33

I was having some issues with Rails 2.1 as well. When the errors were logged, I could not click the link to see the detail. Nothing happened. FireBug was showing me the output as RJS coming back but nothing happened. I followed Antonio Reina's suggestion above to add:

accepts.js { login_required_without_basic }

to my environments/production.rb file and that did the trick. However, I had already installed the RedCloth gem earlier but that would have mattered as well.


23. Anthony Ettinger Jul 05, 2008 at 02:26

bummer that the git stuff doesn't play well with svn.


24. fguillen Jan 16, 2009 at 05:29

I have made a fork of the plugin for jQuery support:

http://github.com/fguillen/exception_logger_jquery/tree/master

I have made just monkey testing.. so I don't know if every thing is correct :)

f.


25. Anthony Ettinger May 19, 2009 at 01:25

I get a $.ajax() error when the plugin calls prototype.js.


26. Andrew Miklas Jul 16, 2009 at 17:00

If you use one of the exception notification plugins mentioned in this episode, but want to receive SMS or phone call alerts instead of email, take a look at PagerDuty (pagerduty.com). It sends automated phone calls or SMS messages in response to any email based trigger. It also lets you route the alerts to different people depending on the day-of-the-week, etc.


27. Nikolich Feb 23, 2010 at 11:01

Good.


28. replica handbags cheap Apr 10, 2010 at 05:14

Thank you


29. till Apr 22, 2010 at 22:59

It is impossible http://ambienbuy.net


30. http://ambienbuy.net May 05, 2010 at 08:07

This is an excellent article, thank you very much for it,
I have found here much useful information in
<a href="http://ambienbuy.net" title="Buy ambien">Buy ambien</a>


31. http://ambienbuy.net May 08, 2010 at 01:02

This is an excellent article, thank you very much for it,
I have found here much useful information in

http://ambienbuy.net


32. Jerry May 23, 2010 at 20:47

Exactly what I was thinking too. I don’t think Dell is going to pay for account to track how many users got to their site through Twitter, when they’ve already done it…for free.
http://welcome-re.ru/


33. tommy May 23, 2010 at 20:48

Your article is really very interesting. This is the information, I’ve been looking for… Thanks
<a href="http://welcome-re.ru/karelia.aspx" title="Недвижимость в Карелии">Недвижимость в Карелии</a>


34. Gary Jun 27, 2010 at 19:30

Truly, your piece goes to the nitty-gritty of the subject. Your clarity leaves me wanting to know more. Just so you know, i will immediately grab your feed to keep up to date with your web site. Sounding Out thanks is simply my little way of saying bravo for a solid resource. Accept my best wishes for your incoming post.


35. Dima Jul 30, 2010 at 22:09

Thanks for article. It's very interesting.


37. hot glue gun Aug 09, 2010 at 00:48

Thanks for this great post. I have become a huge fan of this website and I really cant wait to read you next posts! Your post are inspirational.


38. QuBiT Aug 15, 2010 at 09:18

Today I've rewritten this plugin as a Gem for Rails3.

For more details checkout:

http://rubygems.org/gems/exception_logger

and

http://github.com/QuBiT/exception_logger

And it works ;)

Enjoy and have fun :)


39. authentic nike shoes Aug 19, 2010 at 00:11

This is an excellent article, thank you very much for it, Thanks Ryan for the excellent railscast, as always.


40. Blahnik Shoes Aug 21, 2010 at 01:12

it's a good application. Thanks


41. silly bandz Aug 25, 2010 at 03:31

<A href="http://www.toptoys2trade.com/zhu-zhu-pets-c-52/ ">zhu zhu pets</A>


42. louis vuitton shoes Aug 26, 2010 at 21:14

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


43. snow boots Aug 31, 2010 at 00:25

the problem with the :secret and the love of your work !


44. levis belts Sep 01, 2010 at 20:55

Intimately, the post is actually the best on this laudable topic. I harmonize with your conclusions and will eagerly look forward to your future updates. Saying thanks will not just be adequate, for the fantastic lucidity in your writing.


45. breitling watches uk Sep 02, 2010 at 07:48

The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source