Class: ActiveRecordQueryTrace::ActiveRecord::LogSubscriber
- Inherits:
-
ActiveSupport::LogSubscriber
- Object
- ActiveSupport::LogSubscriber
- ActiveRecordQueryTrace::ActiveRecord::LogSubscriber
- Defined in:
- lib/active_record_query_trace.rb
Instance Method Summary collapse
- #clean_trace(trace) ⇒ Object
-
#initialize ⇒ LogSubscriber
constructor
A new instance of LogSubscriber.
- #sql(event) ⇒ Object
Constructor Details
#initialize ⇒ LogSubscriber
Returns a new instance of LogSubscriber.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_record_query_trace.rb', line 16 def initialize super ActiveRecordQueryTrace.enabled = false ActiveRecordQueryTrace.level = :app ActiveRecordQueryTrace.lines = 5 ActiveRecordQueryTrace.ignore_cached_queries = false if ActiveRecordQueryTrace.level != :app # Rails by default silences all backtraces that match Rails::BacktraceCleaner::APP_DIRS_PATTERN Rails.backtrace_cleaner.remove_silencers! end end |
Instance Method Details
#clean_trace(trace) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_record_query_trace.rb', line 48 def clean_trace(trace) # Rails relies on backtrace cleaner to set the application root directory filter # the problem is that the backtrace cleaner is initialized before the application # this ensures that the value of `root` used by the filter is set to the application root if Rails.backtrace_cleaner.instance_variable_get(:@root) == '/' Rails.backtrace_cleaner.instance_variable_set :@root, Rails.root.to_s end case ActiveRecordQueryTrace.level when :full trace when :rails Rails.respond_to?(:backtrace_cleaner) ? Rails.backtrace_cleaner.clean(trace) : trace when :app Rails.backtrace_cleaner.remove_silencers! Rails.backtrace_cleaner.add_silencer { |line| not line =~ /^(app|lib|engines)\// } Rails.backtrace_cleaner.clean(trace) else raise "Invalid ActiveRecordQueryTrace.level value '#{ActiveRecordQueryTrace.level}' - should be :full, :rails, or :app" end end |
#sql(event) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_record_query_trace.rb', line 29 def sql(event) if ActiveRecordQueryTrace.enabled index = begin if ActiveRecordQueryTrace.lines == 0 0..-1 else 0..(ActiveRecordQueryTrace.lines - 1) end end payload = event.payload return if payload[:name] == 'SCHEMA' return if ActiveRecordQueryTrace.ignore_cached_queries && payload[:name] == 'CACHE' cleaned_trace = clean_trace(caller)[index].join("\n from ") debug(" Query Trace > " + cleaned_trace) unless cleaned_trace.blank? end end |