24

I'm using Rails 4.0.4 with Ruby 2.1 and Thin 1.6.2 on Ubuntu 14.04 through my terminal "Terminator" and my shell "Fish Shell".

When I'm launching my Rails server in development mode I don't have the SQL queries in my logs only the JS and HTML files are loaded.

I'm searching for something like that:

User Load (3.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
(2.0ms)  SELECT COUNT(*) FROM "users" WHERE (driver_register_state_cd = -2)
1
  • By default, you will not see it when booting rails in production mode. Try to start it in development mode. Commented May 1, 2014 at 2:58

3 Answers 3

43

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')
Sign up to request clarification or add additional context in comments.

6 Comments

I forgot to mention that I'm already in development mode... Could a gem make the queries disapear in the logs? Because I didn't touch the environment config files...
where do u want rails to show SQL query? in your local desktop or server? if yes, then which server?
I have updated the answer, please follow it. Please mark is checked if that solve your problem
Thanks, I have upvoted your question. If possible, can you please upvote my answer by clicking on up arrow button. thanks
Its not solve my problem.. Even doing what you told the sql querie still are not showed..
|
8

Set config.log_level in config/environments/development.rb to :debug and restart your local server, console:

 config.log_level = :debug

1 Comment

The default log level is :debug in development mode. So this entry doesn't change anything,
8

I got what I wanted by creating an initializer file:

# initializers/sql_logging.rb
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)

This also seems to keep the logging from having the ugly timestamps prepended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.