Tracing Rails 3 SQL queries
QueryTrace doesn't work as-is because many changes were made in Rails 3 esp in the area of ActiveRecord.
So, hacking around, I made it work like this:
You just need the 2 files below in the locations mentioned. Then restart the web server.
After the SQL, you should see Called from:
in a console (magenta on white) and log file
In /vendor/plugins/query_trace/lib/query_trace.rb
module QueryTrace
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)
logger.debug("\e[1m\e[35m\e[1m\e[47mCalled from:\e[0m " + clean_trace(caller[2..-2]).join("\n "))
end
def clean_trace(trace)
Rails.respond_to?(:backtrace_cleaner) ?
Rails.backtrace_cleaner.clean(trace) :
trace
end
end
In /vendor/plugins/query_trace/init.rb
require 'query_trace'
class ::ActiveRecord::LogSubscriber
include QueryTrace
end
Tracing SQL queries created by Ruby-on-Rails
I think you're looking for QueryTrace.
Retrieving sql queries from Active-record queries in Rails 3
You can debug ActiveRecord queries from a console.
Hit rails console
and enter:
ActiveRecord::Base.logger = Logger.new(STDOUT)
How to get the line of code that triggers a query?
I've found this solution:
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'
logger.debug(" \\_ \e[33mCalled from:\e[0m " + trace_log)
end
end
end
In some initializer add QueryTrace.enable!
Show SQL ActiveRecord queries in rails console using SemanticLogger gem
It depends on the semantic_logger configuration but you should try
SemanticLogger.default_level = :trace
(you can run it in the console, and if works - add it to your initializers/development.rb or local.rb)
In my case I'd now see the output, but as a JSON:
> 2020-02-26 11:26:24.094050 D [2220:11016600] (220.0ms) ActiveRecord --
{ :sql => "SELECT COUNT(*) FROM \"things\"" }
So I need to add in my local.rb (not sure how this can be done dynamically in the console)
Rails.application.configure do
config.rails_semantic_logger.semantic = false
end
to see
2020-02-26 11:31:34.972958 D [6280:14940580] ActiveRecord::Base --
(198.6ms) SELECT COUNT(*) FROM "things"
Log every SQL query to database in Rails
SQL logging in rails -
In brief - you need to override ActiveRecord execute method. There you can add any logic for logging.
Related Topics
How to Implement Total Disjoint Specialization in Database
Complicated SQL Query--Finding Items Matching Multiple Different Foreign Keys
Oracle: Is There Any Logical Reason Not to Use Parallel Execution with Subqueries in the Select List
Efficient Query to Split a Delimited Column into Separate Rows in Another Table
SQL Server Default Date Time Stamp
Oracle SQL - Add Primary Key to Table
How to Determine Position of Row in SQL Result-Set
How SQL's Convert Function Work When Converting Datetime to Float
Row_Number() Over Not Fast Enough with Large Result Set, Any Good Solution
Logging Erroneous Queries Only on SQL Server
How to Retrieve Same Column Twice with Different Conditions in Same Table
How to Use SQL Wildcards in Linq to Entity Framework
Help with Writing a SQL Query for Nested Sets
SQL Query with Union in Doctrine Symfony