Display SQL queries in log with Rails 4
the rails console never writes to the log file, but you can achieve it quite easily, for example, if you execute following after starting the rails console
ActiveRecord::Base.logger = Logger.new STDOUT
rails will log all SQL statements to stdout, thus display them in your terminal. and since Logger.new accepts any stream as first argument, you could just let it write to the rails development.log:
ActiveRecord::Base.logger = Logger.new File.open('log/development.log', 'a')
How to show SQL queries run in the Rails console?
Rails 3+
Enter this line in the console:
ActiveRecord::Base.logger = Logger.new(STDOUT)
Rails 2
Enter this line in the console:
ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)
Rails 4: Show SQL in console in production
ActiveRecord::Base.logger = Logger.new(STDOUT)
Execute it in rails console on your server, and then all the ActiveRecord generated SQL queries will be shown.
Rails: Show SQL Queries in Production Log
In your environments/production.rb
config.log_level = :debug
Remember you need to restart the web server to apply the changes.
Rails Logs: Only show Create, Update, and Destroy SQL statements in Logs and nothing else
You can disable logging of ActiveRecord
database queries by setting the log_level
to :info
.
Then use Rails' ActiveSupport::Instrumentation
and subscribe to the sql.active_record
event.
Just add the following code into an initializer:
# in `config/initializers/query_logger.rb`
class QueryLogger
def call(name, started, finished, unique_id, payload)
query = payload[:sql]
Rails.logger.info("SQL Query: #{query}") unless query.start_with?('SELECT')
end
end
ActiveSupport::Notifications.subscribe('sql.active_record', QueryLogger.new)
How to show SQL query log generated by a RSpec test?
Put the following code in your specs:
Rails 7.0+
ActiveRecord.verbose_query_logs = true
Rails 5.2
ActiveRecord::Base.verbose_query_logs = true
Rails 3.0
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)
Thanks to everyone who commented for the Rails updates.
How do I turn on SQL debug logging for ActiveRecord in RSpec tests?
By default, all your db queries will be logged already in test mode. They'll be in log/test.log
.
How to log Rails queries before they happen?
There is no standard way you can configure logger to output SQL query before execution.
But you can still log queries this way by extending ActiveRecord::LogSubscriber class.
# initializers/extensions/active_record_logger.rb
module Extensions
module ActiveRecordLogger
IGNORE_PAYLOAD_NAMES = ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES
# ActiveRecord::LogSubscriber doesn't implement this method.
# This method will be invoked before event starts processing.
# It's exactly we are looking for!
def start(name, id, payload)
super
return unless logger.debug?
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
name = payload[:name]
sql = payload[:sql]
name = color(name, nil, true)
sql = color(sql, nil, true)
debug "STARTING #{name} #{sql}"
end
end
end
ActiveRecord::LogSubscriber.include Extensions::ActiveRecordLogger
Now you will get query log before execution. For instance, querying
User.find 1
will produce
STARTING User Load SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
#<User id: 1, username: "dimakura", created_at: "2015-09-08 13:16:42", updated_at: "2015-09-08 13:16:42">
Rails - See generated SQL queries in Log files
Yes you can. If you go in the config/environments/production.rb file, there's a section like this:
# See everything in the log (default is :info)
# config.log_level = :debug
Uncomment the config.log_level line, and you'll get the same log in production as you would in dev.
Related Topics
Oracle SQL: Understanding the Behavior of Sys_Guid() When Present in an Inline View
How to Use Group by Based on a Case Statement in Oracle
Updating One Column Based on The Value of Another Column
Parsing Nested Xml into SQL Table
Best Practices for Multithreaded Processing of Database Records
How to Use MySQL Index Columns
How to Find Out If an Oracle Database Is Set to Autocommit
SQL Server - Asynchronous Query Execution
How to Display the Date as Mm/Dd/Yyyy Hh:Mm Am/Pm Using SQL Server 2008 R2
How to Use Left & Right Functions in SQL to Get Last 3 Characters
Order by Month and Year in SQL with Sum
Sql Update Multiple Fields from via a Select Statement
Passing a Dataframe List to a Where Clause in a SQL Query Embedded in R
Varchar(255) V Tinyblob V Tinytext
How to Find What Foreign Key References an Index on Table