Skip to content

Instantly share code, notes, and snippets.

@ruby-fu-ninja
Created May 16, 2014 09:01
Show Gist options
  • Save ruby-fu-ninja/89937db32999742aeb9f to your computer and use it in GitHub Desktop.
Save ruby-fu-ninja/89937db32999742aeb9f to your computer and use it in GitHub Desktop.
Log query line number in Rails console
module QueryTrace
def self.enable!
::ActiveRecord::LogSubscriber.send(:include, self)
end
def self.append_features(klass)
super
klass.class_eval do
unless method_defined?(:log_info_without_trace)
alias_method :log_info_without_trace, :sql
alias_method :sql, :log_info_with_trace
end
end
end
def log_info_with_trace(event)
log_info_without_trace(event)
trace_log = Rails.backtrace_cleaner.clean(caller).first
if trace_log && event.payload[:name] != 'SCHEMA'
puts " \\_ \e[33mCalled from:\e[0m " + trace_log
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment