Reducing log output from the Rails PostgreSQL connection adapter 4

Posted by Tom Copeland Wed, 06 Oct 2010 20:36:00 GMT

When you write a Rails app that uses PostgreSQL you'll probably see your development log filled with log entries like this:

 SQL (0.5ms)   SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
 FROM pg_attribute a LEFT JOIN pg_attrdef d
 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
 WHERE a.attrelid = '"reading_lists"'::regclass
 AND a.attnum > 0 AND NOT a.attisdropped
 ORDER BY a.attnum

This is all boilerplate stuff and definitely not part of your app. So, how to get rid of it? Just use Evgeniy Dolzhenko's silent-postgres gem. Put this in your Rails 3 Gemfile (or add a config.gem entry in your environment.rb file for Rails 2):

group :development, :test do 
  gem 'silent-postgres'
end

And that's it. Cleaner logs, huzzah!

The gem itself is only about 60 lines of code. It creates an initializer that includes itself into ActiveRecord::ConnectionAdapters::PostgreSQLAdapter and uses alias_method_chain to silence output from a couple of methods - tables, pk_and_sequence_for, and a few others. Good stuff!

Comments

Leave a comment

  1. Espen Antonsen about 15 hours later:

    Dude, thanks! This has annoyed me ever since I migrated to Rails3!

  2. Tom Copeland about 16 hours later:

    @espen heh, cool, yeah, it’s nice to clean that up with just a new Gemfile entry.

  3. Jon Atack 1 day later:

    +1, kudos for this solution. The pg logger output annoyed me as well since moving to Rails 3.

  4. Lou Kosak 17 days later:

    Thanks a hojillion, Tom–makes debugging a whole lot easier!

Comments