112

In newer version of Rails, I'm guessing from 3 up, database queries are output to the console. This is useful most of the time, but how can you hide it when you do not want to see it?

0

6 Answers 6

197

A better way of doing this is by typing this into the console:

ActiveRecord::Base.logger.level = 1 

as it prevents problems trying use a pointer to a logger that is set to nil (source: Disable Rails SQL logging in console)

To turn it back on

ActiveRecord::Base.logger.level = 0
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. And to turn it back on, ActiveRecord::Base.logger.level = 0.
Any idea how to do this with Mongoid?
30
ActiveRecord::Base.logger = nil

from here

1 Comment

This can cause NoMethodErrors with ActiveRecord expecting ActiveRecord::Base.logger to be an object rather than nil.
12

Short answer... In the file development.rb change or add the value of config.log_level so that there's a line like

config.log_level = :info

3 Comments

No, this does not seem to have any impact on whats going on in the console. Also, I would prefer a solution that does not require me to change project files.
Ok, the solution works for me with webrick but you might be using other server or running in production or testing modes?
It works for changing the content of the log files. But the question is about the rails console, not the log files.
5

From a friend of mine:

your_query; nil

7 Comments

To improve the quality of your post please include how/why your post will solve the problem.
This is going to stop your ruby console from dumping the results of expressions to console, but it's not going to stop ActiveRecord from dumping sql information to the rails logger.
To the previous commenters: this answers the question, and it's the only answer which worked for me, what more one could wish?
I don't think this answers the question at all. as @eremzeit said, this won't stop all the sql queries from been dumped to your console, it will only stop the return value from that command from being printed...
It doesn't answer the question but useful for not repeating results
|
4

In Rails 3.2, setting

config.logger.level = Logger::INFO

worked fine for me for turning off SQL output.

1 Comment

Does this still log the SQL output to the log file, though?
-2

I see you already got your needed answer although I would like to advise the 'quiet assets' gem to you, most of the log data will be asset compiling and inclusions, this gem will remove that and still output the queries and data behavior.

Have fun

1 Comment

The question is about output to the console, not log files

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.