Class: CacheQueryTrace::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/cache-query-trace.rb

Instance Method Summary collapse

Constructor Details

#initializeLogSubscriber

Returns a new instance of LogSubscriber.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cache-query-trace.rb', line 23

def initialize
  super

  CacheQueryTrace.enabled = false
  CacheQueryTrace.level = :app
  CacheQueryTrace.lines = 5

  if CacheQueryTrace.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

#cache_read(event) ⇒ Object Also known as: cache_write, cache_delete, cache_exist?, read_fragment, write_fragment, exist_fragment?, expire_fragment



36
37
38
39
40
41
# File 'lib/cache-query-trace.rb', line 36

def cache_read(event)
  if CacheQueryTrace.enabled
    cleaned_trace = clean_trace(caller)[CacheQueryTrace.trace_slice].join("\n     from ")
    debug("  ↳ " + cleaned_trace) unless cleaned_trace.blank?
  end
end

#clean_trace(trace) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cache-query-trace.rb', line 52

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 CacheQueryTrace.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| line !~ /^(app|lib|engines)\// }
    Rails.backtrace_cleaner.clean(trace)
  else
    raise ArgumentError, "Invalid CacheQueryTrace.level value '#{CacheQueryTrace.level}' - should be :full, :rails, or :app"
  end
end