Register now and start sharing your code snippets.
-->

How to install the exception_logger Rails plugin and protect the logs with basic authentication

Ruby posted about 1 year ago by christian

This snippet explains how to install and use the Rails exception_logger plugin. I’ll also show you how to protect your logs by extending the plugin with basic authentication.

   1  script/plugin source http://svn.techno-weenie.net/projects/plugins
   2  script/plugin install exception_logger

I’m using Rails Edge on this project, so I had to install classic pagination also:

   1  script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination

Next create and execute the migration file:

   1  ./script/generate exception_migration
   2  rake db:migrate

Before starting the server we need to setup the routes:

   1  map.exceptions '/logged_exceptions/:action/:id', :controller => 'logged_exceptions', :action => 'index', :id => nil 

You also need to include the ExceptionLoggable in your ApplicationController:

   1  class ApplicationController < ActionController::Base
   2    include ExceptionLoggable
   3  ...

Start your server and access the exception log at /logged_exceptions.

Exceptions can contain email addresses, passwords, credit card numbers, so you’ll want to protect /logged_exceptions from the public. This can be done by adding the following code to the end of environment.rb:

   1  config.after_initialize do
   2    require 'application' unless Object.const_defined?(:ApplicationController)
   3    LoggedExceptionsController.class_eval do
   4      before_filter :authenticate
   5  
   6      protected
   7  
   8      def authenticate
   9        authenticate_or_request_with_http_basic do |username, password|
  10          username == "foo" && password == "bar"
  11        end
  12      end
  13    end
  14  end

With this code we add a before filter that shows a login dialog to anyone trying to access /logged_exception/. Note that this requires Rails 2.0 basic authentication to work, so make sure you have the proper version installed.

Tagged ruby, exception_logger, install, routes, rails, basic, authentication