111

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?

Roger Ertesvag
  • 1,784
  • 2
  • 14
  • 15

6 Answers6

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
30
ActiveRecord::Base.logger = nil

from here

Community
  • 1
  • 1
samvermette
  • 40,269
  • 27
  • 112
  • 144
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
  • 1
    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. – 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
  • 3
    It 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
  • 1
    To improve the quality of your post please include how/why your post will solve the problem. – Mick MacCallum Oct 06 '12 at 06:35
  • 7
    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. – eremzeit Sep 05 '13 at 04:27
  • 1
    To 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
  • 3
    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... – opsidao Sep 03 '14 at 13:26
  • 1
    It doesn't answer the question but useful for not repeating results – Rutger Dec 16 '15 at 09:44
  • Ah this is huge. Thanks. – Abram Sep 06 '16 at 14:14
  • 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
  • 1
    Returning 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