Rails console is not outputting SQL Statements to my Development Log
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')
Development.log log file isn't logging Rails SQL queries
Apparently showing the SQL in the console rather than development.log is default behavior for Rails 3.1. I haven't found a configuration option for changing that behavior, but I have found that, once the console is running, you can just do:
irb(main):001:0> ActiveRecord::Base.logger = Rails.logger
and that will take the sql out of the console and put it back in development.log. Or, if you don't want to do that every time you fire up the console, you can edit gems/railties-(version)/lib/rails/console.rb, and make the above assignment after the line in the start method that reads:
@app.load_console
Not saying it's a good solution, but it'll tide me over until I find something better...
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)
In the rails 4 console, how do I log all sql activity to file BUT not to the console output?
This ActiveRecord::Railtie code is what says to broadcast logger messages to the console:
# When loading console, force ActiveRecord::Base to be loaded
# to avoid cross references when loading a constant for the
# first time. Also, make it output to STDERR.
console do |app|
require "active_record/railties/console_sandbox" if app.sandbox?
require "active_record/base"
console = ActiveSupport::Logger.new(STDERR)
Rails.logger.extend ActiveSupport::Logger.broadcast console
end
So one thing you could do when you first get into the console is to set ActiveRecord::Base.logger
to a new logger that does not broadcast its messages to the console. For example:
ActiveRecord::Base.logger = ActiveSupport::Logger.new("log/development.log")
"log/development.log" can be changed to wherever you want to send the SQL output.
Rails SQL insert statements not showing up in console
Put this in either in your .irbrc or .pryrc in your home directory
if defined?(Rails) && !Rails.env.nil?
puts '... ActiveRecord and ActiveResource Logger set to STDOUT'
logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = logger
ActiveResource::Base.logger = logger
end
You should start to see the sql statements executing in the rails console.
Hope this helps
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')
Disable Rails SQL logging in console
To turn it off:
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
To turn it back on:
ActiveRecord::Base.logger = old_logger
Related Topics
How to Strip Commas from Float Input
Rspec - How to Test Activerecord::Recordnotfound
If Referer = "/Search", Do X, Else Do Y: "Bad Argument (Expected Uri Object or Uri String)"
Does Will Pagination Work with Forms Which Have Method="Post"
Pow Not Loading Gem Properly While Rails S Works
How to Add "Access-Control-Allow-Origin" Headers to API Response in Ruby
Mongomapper Many to Many Issue with Array
Proc Throws Error When Used with Do End
How to Use Link_To with Ajax in Rails 3.2.1
Rails: How to Use Scope to Find an Element in Array of Arrays
Define a Class Method in a Module
Save Google Cloud Speech API Operation(Job) Object to Retrieve Results Later
Sum of All Amount If Charge Dates Are the Same in Stripe
How to Display Error Messages in a Multi-Model Form with Transaction
Cannot Require 'Nokogiri' in Rails (But Works in Irb)
Rails 3 + Daemons Gem: Exception When Querying Model
Rails: How to Pass Custom Params to a Controller Method
Installed Memcached via Homebrew, How to Start and Stop Server