Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import.

git-svn-id: https://terralien.devguard.com/svn/projects/plugins/query_trace@12 62287d4f-6109-0410-89f5-c3c281edf842
  • Loading branch information...
commit 2280a1b2b558750944d1f702184b9ff7fbd7d086 0 parents
nathaniel authored
Showing with 146 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +79 −0 README
  3. +5 −0 init.rb
  4. +42 −0 lib/query_trace.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 Nathaniel Talbott. All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
79 README
@@ -0,0 +1,79 @@
+= QueryTrace plugin for Rails
+
+It's nice that ActiveRecord logs the queries that are performed when your actions are executed,
+since it makes it easy to see when you have serious inefficiencies in your application. The next
+question, though, is always, "OK, so where are those being run from?" Before QueryTrace, that
+question could be a real pain to answer, since you'd have to go trawling through your code looking
+for the culprit. Once you have QueryTrace installed, though, your logs won't just tell you that you
+have a problem, they will pinpoint the location of that problem for you.
+
+== Usage
+
+All you have to do is have the plugin installed - QueryTrace takes care of the rest, including:
+
+* Only displaying when at the DEBUG log level
+* Honoring your log colorization settings
+
+== Example
+
+Before:
+
+ Schedule Load (0.023687) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
+ Resource Load (0.001076) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
+ Schedule Load (0.011488) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
+ Resource Load (0.022471) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
+
+
+After:
+
+ Schedule Load (0.023687) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
+ app/models/available_work.rb:50:in `study_method'
+ app/helpers/plan_helper.rb:4:in `work_description'
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
+ Resource Load (0.001076) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
+ app/models/available_work.rb:54:in `div_type'
+ app/helpers/plan_helper.rb:6:in `work_description'
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
+ Schedule Load (0.011488) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
+ app/models/available_work.rb:50:in `study_method'
+ app/helpers/plan_helper.rb:4:in `work_description'
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
+ Resource Load (0.022471) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
+ app/models/available_work.rb:54:in `div_type'
+ app/helpers/plan_helper.rb:6:in `work_description'
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
+
+
+== Additional Info
+
+Author: Nathaniel Talbott (for Terralien)
+Contact: nathaniel@talbott.ws
+License: MIT
+Home: http://terralien.com/svn/general/plugins/query_trace/
+
+
+Copyright (c) 2006 Nathaniel Talbott. All Rights Reserved.
5 init.rb
@@ -0,0 +1,5 @@
+require 'query_trace'
+
+class ::ActiveRecord::ConnectionAdapters::AbstractAdapter
+ include QueryTrace
+end
42 lib/query_trace.rb
@@ -0,0 +1,42 @@
+module QueryTrace
+ def self.included(klass)
+ klass.class_eval %(
+ alias_method :log_info_without_trace, :log_info
+ alias_method :log_info, :log_info_with_trace
+
+ def row_even
+ @@row_even
+ end
+ )
+ end
+
+ def log_info_with_trace(sql, name, runtime)
+ log_info_without_trace(sql, name, runtime)
+
+ return unless @logger and @logger.debug?
+ return if / Columns$/ =~ name
+
+ trace = clean_trace(caller[2..-1])
+ @logger.debug(format_trace(trace))
+ end
+
+ def format_trace(trace)
+ if ActiveRecord::Base.colorize_logging
+ if row_even
+ message_color = "35;2"
+ else
+ message_color = "36;2"
+ end
+ trace.collect{|t| " \e[#{message_color}m#{t}\e[0m"}.join("\n")
+ else
+ trace.join("\n ")
+ end
+ end
+
+ VENDOR_RAILS_REGEXP = %r(([\\/:])vendor\1rails\1)
+ def clean_trace(trace)
+ return trace unless defined?(RAILS_ROOT)
+
+ trace.select{|t| /#{Regexp.escape(RAILS_ROOT)}/ =~ t}.reject{|t| VENDOR_RAILS_REGEXP =~ t}.collect{|t| t.gsub(RAILS_ROOT + '/', '')}
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.