378

Sprockets tends to be quite verbose in the (dev) log by default under Ruby on Rails 3.1 (RC1):

Started GET "/assets/application.css" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Compiled app/assets/stylesheets/application.css.scss  (5ms)  (pid 6303)


Started GET "/assets/application.js" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Compiled app/assets/stylesheets/default.css.scss  (15ms)  (pid 6303)

...
Started GET "/assets/default/header_bg.gif" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Served asset /default/header_logo.gif - 304 Not Modified  (7ms)  (pid 6303)
Served asset /default/header_bg.gif - 304 Not Modified  (0ms)  (pid 6246)
Served asset /default/footer_bg.gif - 304 Not Modified  (49ms)  (pid 6236)
...

I'd like to either reduce the level of verbosity or disable it altogether.

I'm assuming there is a clean way to disable or reduce the verbosity of the logging by adding a config line in either environment.rb or development.rb similar to config.active_record.logger = nil which silences ActiveRecord SQL statements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
istvanp
  • 4,423
  • 3
  • 24
  • 26
  • Someone reported a bug about this: [#2639](https://github.com/rails/rails/issues/2639). Still "open" as of 9/2. – istvanp Sep 02 '11 at 06:08
  • 14
    The accepted answer for this question should be changed or updated. In Rails 3.2 you can just put `config.assets.debug = false` in your development.rb. – Stewart Johnson Apr 16 '13 at 10:41
  • 7
    @StewartJohnson - `config.assets.debug = false` will concatenate assets into a single file- not what most people want in development – Yarin Jan 03 '14 at 17:09

14 Answers14

381

Place the following code in config/initializers/quiet_assets.rb

if Rails.env.development?
  Rails.application.assets.try(:logger=, Logger.new('/dev/null'))
  Rails::Rack::Logger.class_eval do
    def call_with_quiet_assets(env)
      previous_level = Rails.logger.level
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

Updated: It now works for Ruby on Rails 3.2 too (previous attempt fixes before_dispatch, and now we're going for the root rack call instead)

Update: A proper Rack middleware solution (instead of fragile alias_method_chain) from @macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
choonkeat
  • 5,557
  • 2
  • 26
  • 19
  • Wonderful - until #2639 is fixed this is the only solution I've found so far that works. – Michael Shimmins Nov 23 '11 at 08:52
  • Looks like Rails 3.2 will have an easier way to do this: https://github.com/rails/rails/pull/4501 https://github.com/rails/rails/pull/4512 – ootoovak Jan 18 '12 at 09:05
  • 3
    As of Rails 3.2.1 those pulls alone do not squelch the sprocket logging and choonkeat's file is still needed. – IAmNaN Jan 27 '12 at 22:10
  • 5
    Indeed. Sounds like this file will always be needed, as setting `config.assets.logger` to false will only silence what Sprockets outputs. This silences Action Pack requests/responses, which is something that the Rails dev [have said they don't intend to silence for special cases](https://github.com/rails/rails/pull/3795#issuecomment-3549669). – Ben Kreeger Mar 14 '12 at 15:06
  • 33
    uhm. you should just get this into rails core. make it an option in config.assets – jsharpe Apr 11 '12 at 13:14
  • It would be better to disable logging than to log to /dev/null: `Rails.application.assets.logger = false` – sj26 Apr 23 '12 at 05:55
  • It would also be nice if sprockets were conditionally inserted into the `ActionDispatch::MiddlewareStack` instead of mounted as a route, then we could simply move it up the stack before `Rails::Rack::Logger`. – sj26 Apr 23 '12 at 05:56
  • 1
    No longer works on latest rails version 3.2.5 how can we fix this? thx! – Rubytastic Jun 08 '12 at 07:03
  • @Rubytastic works for me. what are your errors? please put on https://gist.github.com/ – choonkeat Jun 08 '12 at 15:15
  • Works on 3.2.5. Teh amazing. Thanks! – c.apolzon Jun 21 '12 at 04:36
  • obviously not threadsafe; `alias_method_chain` is redundant - plain `alias` would be enough; but many many thanks for pointing at `Rails::Rack::Logger`! =) – meandre Mar 18 '13 at 09:05
  • 2
    On windows replace `'/dev/null'` with '`NUL`' – Matt Apr 28 '13 at 20:57
  • works for me on rails4 but agree with @crankharder. this should go into config.assets – Snowcrash May 28 '13 at 18:38
  • 4
    Works for me on Rails 4.2.0 – hsym Feb 18 '15 at 10:21
  • For us, this didn't survive an upgrade to Rails 4.2.5. The gem mentioned below still works, though. I suspect this solution will still work if you lock sprockets in your Gemfile to some specific older version, but I'd rather not have to. – johncip Feb 01 '16 at 00:39
  • 1
    @johncip you can skip overwriting `Rails.application.assets.logger`, have updated the code – choonkeat Feb 01 '16 at 12:29
189

Take a look at https://github.com/evrone/quiet_assets and just include it into your Gem file.

For the lazy: gem 'quiet_assets', group: :development

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
route
  • 1,954
  • 1
  • 11
  • 8
  • 6
    Great work, but very sad that a separate gem is required for this. – Adam Spiers Mar 04 '13 at 00:57
  • 1
    I think Jose Valim made right decision here https://github.com/rails/rails/issues/2639 rails has to log all incoming request and I agree with that, I think we can get rid of this overhead when sprockets will support source map https://github.com/sstephenson/sprockets/issues/310 – route Apr 17 '13 at 07:34
  • 5
    for the lazy: `gem 'quiet_assets'` (please add this to the post :)) – reto Mar 16 '14 at 10:22
52

For Ruby on Rails 3.2, add config.assets.logger = false to your development environment configuration file, typically found at config/environments/development.rb. See #4512.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ouranos
  • 1,224
  • 10
  • 16
28

Two things are enough:

  1. config.assets.debug = false in config/enviroments/development.rb
  2. rake assets:precompile. See comment by @oma below; this is not needed

That's all!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lisovsky Vlad
  • 448
  • 5
  • 5
  • 19
    1. is correct. Thanks! Please remove No 2. `rake assets:precompile` is not something we wanna do in development – oma Jan 25 '12 at 15:28
  • While this probably didn't work at the time the original question was posted it works now (and, as @Race stated, witl 3.2.3 already) and should therefire be the accepted answer now. – radiospiel Feb 25 '13 at 16:01
  • 2
    As istvanp points out below, that doesn't do what you think it does. It only compiles all JS and CSS assets into a single large file - it doesn't turn off logging for assets. – davidgoli Sep 24 '13 at 21:25
  • 2
    This is all that is needed in rails 4.2.2 – thisfeller Aug 19 '15 at 14:30
  • Adding to what @davidgoli said: config.assets.debug controls asset concatenation. Turning that off means that debugging e.g. JS and CSS using the browser will become more work. Something like quiet_assets will squelch the logging without causing you to have to toggle assets.debug to, well, debug. – johncip Feb 01 '16 at 00:33
27

Eventually, it will be config.assets.logger = nil, but that part is currently stubbed on master (not done yet).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
colinross
  • 2,075
  • 13
  • 10
  • 5
    Doesn't work for me either on Rails 3.1.3. @nessur: are you *sure* it works for you? As Tim says, [#2639](https://github.com/rails/rails/issues/2639) is still open, and I see no indication in that issue of any fix (both referenced pull requests were rejected). – Adam Spiers Jan 12 '12 at 12:28
  • This will not be implemented as this stage. https://github.com/rails/rails/issues/4569 – 23inhouse Sep 09 '12 at 03:47
  • @AdamSpiers as per the link: https://github.com/rails/rails/pull/3795#issuecomment-3549669 "Rails won't special case any of the loggers or logger related middlewares to not log specific routes" – 23inhouse Mar 05 '13 at 01:21
  • @23inhouse I already saw that, but [as I already said](https://github.com/rails/rails/pull/3795#issuecomment-14360177) it's just a statement of intent, not a justification for that intent. – Adam Spiers Mar 05 '13 at 15:37
  • 3
    Bummer. Rails 4 still can't easily disable asset logging. – Tom Rossi Sep 14 '13 at 15:29
  • 1
    I'm using Rails 4.2, put this in my `development.rb`, and it disabled logging of assets. – Jack Oct 11 '15 at 19:12
12

Many people are confused about the use of config.assets.logger = false. Here is what it does and what it doesn't do.

According the source documentation:

Setting config.assets.logger to false will turn off served assets logging.

However this probably is not what you think it is. It only disables sprocket 'serving' logs, not Ruby on Rails actionpack request logs. The Ruby on Rails maintainer explains this clearly here: https://github.com/rails/rails/issues/4569#issuecomment-3594500


Taking example from the link, logs like this are disabled:

Served asset /jquery.isotope.js - 304 Not Modified (0ms)

But logs like this are not

Started GET "/assets/jquery.isotope.js?body=1" for 127.0.0.1 at 2012-01-20 23:16:46 -0500

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lulalala
  • 17,572
  • 15
  • 110
  • 169
12

I know it's an ugly and temporary solution, but I use this:

tail -f log/development.log | grep -vE 'asset'

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sucrenoir
  • 2,994
  • 1
  • 27
  • 31
  • 9
    Here's an improved one that removes blank lines as well: `tail -f log/development.log | grep -vE "(^\s*$|asset)"` – istvanp Sep 09 '11 at 06:12
10
config.assets.quiet = true

This is the latest way to go.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
7

In file development.rb in config/environments you'll find the line config.assets.debug = true.

Switch that to false and most of the asset load output will be gone. On my system only the two requests, for application.css and .js, remain.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TKAB
  • 174
  • 1
  • 7
  • 3
    That setting only allows you to break the stylesheets and javascript into separate files when set to true for easier debugging. When set to false (the default) it bundles them all into one big file. So it does indeed reduce debug output but if you have images for example, those are not affected at all. Official guide info [here](http://edgeguides.rubyonrails.org/asset_pipeline.html#turning-debugging-off). – istvanp Sep 08 '11 at 05:37
  • 1
    I see. Thanks for clearing that up. But I did not change the setting of this variable, so my default was `true`. – TKAB Sep 10 '11 at 08:40
7

Use:

Rails.application.assets.logger = Logger.new(RUBY_PLATFORM =~ /(win|w)32$/ ? "NUL" : "/dev/null")
Rails::Rack::Logger.class_eval do
  def call_with_quiet_assets(env)
    previous_level = Rails.logger.level
    Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
    call_without_quiet_assets(env).tap do
      Rails.logger.level = previous_level
    end
  end
  alias_method_chain :call, :quiet_assets
end

It's the same code choonkeat added. I just included it to work under Windows as well.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Celso Dantas
  • 1,181
  • 10
  • 14
5

In file config/environments/development.rb please add:

config.assets.debug = false

config.assets.logger = false
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Najam Tariq
  • 51
  • 1
  • 3
2

Lograge for the win - it kills Ruby on Rails' annoying logger defaults out of the box (e.g. logging assets, logging partial rendering) and is customizable if you want to add/remove specific items.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yarin
  • 173,523
  • 149
  • 402
  • 512
0

The previously mentioned linked solution helps:

https://github.com/evrone/quiet_assets

Also as below, it's working fine for me:

3.1 (only) (3.2 breaks before_dipatch)

app\config\initializers\quiet_assets.rb

Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
  def before_dispatch_with_quiet_assets(env)
    before_dispatch_without_quiet_assets(env) unless env['PATH_INFO'].index("/assets/") == 0
  end
  alias_method_chain :before_dispatch, :quiet_assets
end
3.2 Rails - Rack root tap approach
app\config\initializers\quiet_assets.rb

Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
  def call_with_quiet_assets(env)
    previous_level = Rails.logger.level
    Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
    call_without_quiet_assets(env).tap do
      Rails.logger.level = previous_level
    end
  end
  alias_method_chain :call, :quiet_assets
end
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumit Munot
  • 3,748
  • 1
  • 32
  • 51
-1

In config/environments add config.log_level = :error to the .rb files you want to change. This will change the log settings to error only.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris O
  • 17
  • Unfortunately no because I mostly use the log for looking at the request and SQL data which are at the debug level. Asset messages are of info level (which is lower than the debug level) so it's no use getting what I want with that setting. – istvanp Sep 02 '11 at 05:13
  • 2
    This is not a good idea. It will hide other info level messages that you might want to still be logging. – ifightcrime Jun 05 '13 at 14:45