Fork me on GitHub

August 30, 2009

Trimming BLOBs from Rails Log

Post moved to http://log2.kares.org/post/59891226819/trimming-blobs-from-rails-log

If you're using some BLOB data in ActiveRecord (e.g. user pictures stored in DB), then you may have noticed that the Rails logging simply prints the INSERT/UPDATE SQL containing the raw binary content. Now for me (at least in development env) this is pretty useless and besides my terminal seems to be getting crazy out of it.
As usual I thought someone must have done it before and found this post. It's pretty outdated since it targets Rails 1.x, so I decided to make it work with Rails 2.x and here's what I got :



Put this monkey-patch into a file named trim_blob_logging.rb into your RAILS_ROOT/lib directory. Then require 'trim_blob_logging' in environments/development.rb.

Now, instead of being bloated with binary bits, your blob log entries should look similar to this one:


INSERT INTO db_files (data) VALUES(x'ffd8ffe000104a464946000101010048... (14133 bytes)')


As a home work you might further improve your SQL logs with stats (bytes transferred from DB per request), it's all in here done for Rails 1.x.

Thanx http://gurge.com/blog for the inspiration !

3 comments:

  1. Perfect! Just what I was looking for!

    ReplyDelete
  2. Did You manage to do the same thing for rails 3?

    ReplyDelete
  3. @Anonymous there's no format_log_entry in Rails 3 but there's still the log method one could monkey-patch - I've updated the gist - should work with Rails 3+ (although I only tested on 2.3).

    ReplyDelete