Plugins - sql_logging

StarAdd to favorites

SQL Logging plug-in

This SQL Logging plug-in combines three useful SQL debugging tools into one easy to use plug-in:

  • Adam Doppelt’s enhancements to the standard logging
  • Nathaniel Talbott’s QueryTrace plug-in
  • silencing the query log entirely for code within a block

This plug-in expands and improves on these ideas in some significant ways, and works on both MySQL and PostgreSQL.

Usage

Once installed, the out-of-the-box behavior gives you:

  • a cleaned-up backtrace after every SQL query, so you know exactly what piece of code triggered the query
  • a summary of the number of rows and rough count of bytes returned by each query
  • after each request completes, additional summary information telling you the number of selects and executes, as well as a rough total number of bytes received from the database
  • a top 10 list of most frequently executed queries (based on the location of the query in code), including the total number of rows and bytes returned for each unique query

All of this additional information is logged at DEBUG level, so it won’t clutter up your production log files unless you’ve changed the log level.

Options

The Top 10 summary provides the most options to customize its behavior.

If you want more or fewer than 10 queries, you can easily change that:

  ActionController::Base.top_sql_queries_shown = 5

If you want the sorted by something other than the number of times a query executed, you can instead sort by the number of rows or bytes returned over all executions of the query:

  ActionController::Base.show_top_sql_queries = :rows
  ActionController::Base.show_top_sql_queries = :bytes

You can also turn the list off entirely:

  ActionController::Base.show_top_sql_queries = false

If you don’t want to ever see query backtraces:

  ActiveRecord::Base.log_sql_backtrace = false

If you don’t want to ever see queries as they execute or their backtraces:

  ActiveRecord::Base.log_sql_query = false

Silencing the logging only sometimes

Sometimes, it’s useful to silence some of the logging for just a particular section of code. There may be useful log statements in the controller or model that are overshadowed by the sheer number of log statements coming from ActiveRecord queries.

There are two methods that will silence the logging of a query entirely, or just the backtrace, for the duration of a block:

  silence_sql do
    # ...
  end

  silence_sql_backtrace do
    # ...
  end

These are available in all controllers and ActiveRecord models.

Caveats

I have not yet tested this code on Rails 2.0.

I’m not yet using ActiveResource, so it may do nothing for you there.

Acknowledgments

Adam’s post describing his logging improvements is at http://gurge.com/blog/2006/11/09/rails-sql-logging-improvements/.

Nathaniel’s QueryTrace is available at http://terralien.com/projects/querytrace/.

Contact

Feedback and/or patches to make this plug-in better are greatly appreciated!

Steve Madsen Light Year Software, LLC http://lightyearsoftware.com steve@lightyearsoftware.com

Steve Madsen

http://svn.lightyearsoftware.com/svn/plugins/sql_logging

Rails' (MIT)

  • Currently 5.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Statistics and Logs

Tags

Comments

Add a comment
Rob Biedenharn 23 Feb 2008

@Larry, Perhaps that's because you didn't read to the end of the posting where Steve wrote <quote>I have not yet tested this code on Rails 2.0.</quote>

Larry 19 Feb 2008
I just installed your plugin (Rails 2.0.2, MySQL 5.0) and got this error message when I fired up Mongrel: Exiting C:/Programs/ruby-1.8.6/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method': undefined method `query' for class `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' (NameError) from C:/Programs/ruby-1.8.6/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain' from C:/stupidtuesday/vendor/plugins/sql_logging/lib/sql_logging/postgresql.rb:10 from C:/Programs/ruby-1.8.6/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require' from C:/Programs/ruby-1.8.6/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require' from C:/Programs/ruby-1.8.6/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require' from C:/Programs/ruby-1.8.6/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in' from C:/Programs/ruby-1.8.6/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require' from C:/stupidtuesday/vendor/plugins/sql_logging/lib/sql_logging/summary.rb:120 ... 44 levels...

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.
Recruiting software

Have a comment?