#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 26 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. Falk May 05, 2008 at 08:21

Like always, thanks a lot for your effort!


4. 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.


5. 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)!


6. 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.


7. 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.


8. kino May 05, 2008 at 13:03

maybe will_paginate portage to git?


9. 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.


10. Leland May 05, 2008 at 14:10

Rocking the cas bah. Thanks.


11. 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


12. 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.


13. 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!


14. 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.


15. 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.


16. 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)


17. 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.


18. 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)


19. 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/


20. 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?


21. Federico Jun 04, 2008 at 10:00

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


22. 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.


23. 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?


24. 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.


25. 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.


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

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

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