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?
Asked
Active
Viewed 3.7k times
6 Answers
193
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

Soviut
- 88,194
- 49
- 192
- 260

Aaron B. Russell
- 2,416
- 1
- 19
- 22
-
33Thanks. And to turn it back on, `ActiveRecord::Base.logger.level = 0`. – thebenedict Nov 19 '13 at 08:34
-
Any idea how to do this with Mongoid? – Jesse Farmer Aug 21 '17 at 03:05
30
ActiveRecord::Base.logger = nil
from here

Community
- 1
- 1

samvermette
- 40,269
- 27
- 112
- 144
-
6This can cause `NoMethodError`s with ActiveRecord expecting `ActiveRecord::Base.logger` to be an object rather than `nil`. – Aaron B. Russell Jul 16 '13 at 11:55
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

madth3
- 7,275
- 12
- 50
- 74
-
1No, 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. – Roger Ertesvag Oct 13 '11 at 10:20
-
Ok, the solution works for me with webrick but you might be using other server or running in production or testing modes? – madth3 Oct 21 '11 at 00:48
-
3It works for changing the content of the log files. But the question is about the rails console, not the log files. – Roger Ertesvag Oct 22 '11 at 16:45
5
From a friend of mine:
your_query; nil

Josh Darnell
- 11,304
- 9
- 38
- 66

tatiCarvalho
- 115
- 1
- 1
-
1To improve the quality of your post please include how/why your post will solve the problem. – Mick MacCallum Oct 06 '12 at 06:35
-
7This 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. – eremzeit Sep 05 '13 at 04:27
-
1To the previous commenters: this answers the question, and it's the only answer which worked for me, what more one could wish? – valk Mar 21 '14 at 04:19
-
3I 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... – opsidao Sep 03 '14 at 13:26
-
1
-
-
This help me to clear logs from awfull 200 ln raw sql queries (yeah my predecessor was a PIG). Thx a lot man – plombix Apr 19 '17 at 16:46
-
1Returning nil will only prevent the result from returning. This doesn't prevent the actual SQL from being logged to the console. The original question was how to suppress the SQL from being logged. – jeremywoertink Jul 14 '17 at 17:31
4
In Rails 3.2, setting
config.logger.level = Logger::INFO
worked fine for me for turning off SQL output.

brokenbeatnik
- 720
- 6
- 15
-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

dennis
- 2,000
- 18
- 26