Ruby-on-rails 如何在 Rails 3.1 中禁用资产管道(链轮)消息的日志记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6312448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable logging of asset pipeline (sprockets) messages in Rails 3.1?
提问by istvanp
Sprockets tends to be quite verbose in the (dev) log by default under Rails 3.1 (RC1):
默认情况下,在 Rails 3.1 (RC1) 下,链轮在 (dev) 日志中往往非常冗长:
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.rbor development.rbsimilar to config.active_record.logger = nilwhich silences ActiveRecord SQL statements.
我想降低详细程度或完全禁用它。我假设有一种干净的方法来禁用或减少日志记录的详细程度,方法是在其中添加一个配置行environment.rb或development.rb类似的配置行来config.active_record.logger = nil使 ActiveRecord SQL 语句静音。
回答by choonkeat
Place the following code in config/initializers/quiet_assets.rb
将以下代码放入 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: now works for Rails 3.2 too (previous attempt fixes before_dispatchnow we're going for the root rack callinstead)
更新:现在也适用于 Rails 3.2(以前的尝试修复before_dispatch现在我们将call改为使用根机架)
Update: A proper Rack middleware solution (instead of fragile alias_method_chain) from @macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735
更新:alias_method_chain来自@macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735 的适当机架中间件解决方案(而不是脆弱的)
回答by route
Take a look at https://github.com/evrone/quiet_assetsand just include it into your Gemfile
看看https://github.com/evrone/quiet_assets并将其包含在您的 Gemfile 中
For lazy: gem 'quiet_assets', group: :development
对于懒人: gem 'quiet_assets', group: :development
回答by ouranos
回答by Lisovsky Vlad
Two things is enough:
1. config.assets.debug = falsein config/enviroments/development.rb
2. rake assets:precompile. See comment by @oma below; this is not needed
两件事就足够了:
1.config.assets.debug = false在config/enviroments/development.rb
2. rake assets:precompile. 请参阅下面@oma 的评论;这是不需要的
Thats all!
就这样!
回答by colinross
Eventually, it will be config.assets.logger = nilbut that part is currently stubbed on master (not done yet)
最终,它将是config.assets.logger = nil但该部分目前在 master 上存根(尚未完成)
回答by Sucrentheitroad
I know it's an ugly and temporary solution but i use this :
我知道这是一个丑陋的临时解决方案,但我使用这个:
tail -f log/development.log | grep -vE 'asset'
tail -f 日志/development.log | grep -vE '资产'
回答by lulalala
Many people are confused about the use of config.assets.logger = false. Here is what it does and what it doesn't do.
很多人对config.assets.logger = false. 这是它做什么和不做什么。
According the source documentation:
根据源文件:
Setting
config.assets.loggerto false will turn off served assets logging.
设置
config.assets.logger为 false 将关闭服务资产日志记录。
However this probably is not what you think it is. It only disables sprocket 'serving' logs, not Rails actionpack request logs. The Rails maintainer explains this clearly here: https://github.com/rails/rails/issues/4569#issuecomment-3594500
然而,这可能不是你想的那样。它只禁用 sprocket 'serving' 日志,而不是 Rails actionpack 请求日志。Rails 维护者在这里清楚地解释了这一点: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)
服务资产 /jquery.isotope.js - 304 未修改 (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
在 2012-01-20 23:16:46 -0500 开始 GET "/assets/jquery.isotope.js?body=1" for 127.0.0.1
回答by Adam Waite
config.assets.quiet = true
This is the latest way to go.
这是最新的方法。
回答by Celso Dantas
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 to work under windows as well.
这是@choonkeat 添加的相同代码。我也包括在 Windows 下工作。
回答by TKAB
in development.rb in config/environments you'll find the line config.assets.debug = true. Switch that to falseand most of the asset load output will be gone. On my system only the two requests for application.css and .js remain.
在 config/environments 中的 development.rb 中,您会找到行config.assets.debug = true. 将其切换到false大部分资产负载输出将消失。在我的系统上,只剩下对 application.css 和 .js 的两个请求。

