Rails 禁用 Rails 3.1 烦人的 assets 请求日志

huacnlee · 2011年11月21日 · 最后由 i5ting 回复于 2013年09月20日 · 9134 次阅读

Rails 3.1 为我们带来了 Asset Pipeline,同时也带来了烦人的 asset log 在 Rails 控制台里面,尤其是项目大,JS,CSS 小文件多的情况,都没法认真的关注查询 log 了...

Started GET "/topics" for 127.0.0.1 at 2011-11-21 15:15:29 +0800
  Processing by TopicsController#index as HTML
MONGODB ruby_china['topics'].find({}).sort([[:_id, :desc]])
Rendered topics/_base.html.erb (1.1ms)
Rendered topics/index.html.erb within layouts/application (30.9ms)
Completed 200 OK in 58ms (Views: 53.9ms)
Served asset /bootstrap.css - 304 Not Modified (0ms)
Served asset /application.css - 304 Not Modified (1ms)
Served asset /front.css - 304 Not Modified (0ms)
Served asset /jquery.jdialog.css - 304 Not Modified (0ms)
Served asset /jquery.js - 304 Not Modified (0ms)
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
Served asset /jquery.jdialog.js - 304 Not Modified (0ms)
Served asset /jquery.tipsy.js - 304 Not Modified (0ms)
Served asset /will_paginate.js - 304 Not Modified (0ms)
Served asset /bootstrap-tabs.js - 304 Not Modified (0ms)
Served asset /application.js - 304 Not Modified (0ms)
Served asset /logo_big.png - 304 Not Modified (10ms)

赶紧禁用它吧!

config/environments/development.rb 里面加入

YouApp::Application.configure do
  ...
  # 加入这个
  config.after_initialize do |app|
    app.assets.logger = Logger.new('/dev/null')
  end
end

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

从此世界清净了!

关于这个,Rails Issue 里面又很多人在说了: https://github.com/rails/rails/issues/2639

不错~ 早就烦透了这东西了

我也觉得这个东西好烦

好东东

嗯,解决了大问题

Rails 3.2 以上直接

config.assets.logger = false

就可以了。

#7 楼 @fredwu

这个貌似还不管用,最后还得用 lz 的代码活着那个 gem

#7 楼 @fredwu 在 Rails 3.2.1 中试了下,没有效果。

#7 楼 @fredwu #10 楼 @daqing 那个配置对 Started GET ...这一行是无关的。

果断使用 +1

Less clever, but maybe nice to know you can also use grep

tail -f log/development.log|grep --context=3 some_string_you_want

or

tail -f log/development.log|grep -v some_string_you_dont_want_to_see

for more info, check this screencast on grep

http://happycasts.net/episodes/27

#15 楼 @happypeter --context=3 只能显示附近的 3 行,有没有办法自动确定 context 长度吗?

tail -f log/development.log | grep -vE "(^\s*$|asset)"  

能隐藏 asset,如果能在每个 request 后面显示一个空行就完美了。

如果是开发环境,我觉得这个更方便

rails s | grep -vE "(^\s*$|asset)"

#16 楼 @camel

有没有办法自动确定 context 长度吗? I think you can if you make your boundaries clear with regexp, don't know how.

If we really want tight control, write a formater maybe

rails s|my_formater.rb

So all I can give here is hollow guesses, but I've found this helpful: http://railscasts.com/episodes/56-the-logger-revised

懒人直接用 thin,没有这些输出

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

里面说3.2是这个代码

tail -n0 -f $RAILS_ROOT/log/development.log | sed -e '{/^$/N;/^\n$/D};{/Started GET "\/assets\//d}'

去除了 assets 提示,同时,将多个连续的空白行换成了一行,让输出更紧凑。

话说 7 个月前已经 close 了,为啥 3.2.13 还不好用呢?

https://github.com/rails/rails/issues/4569

需要 登录 后方可回复, 如果你还没有账号请 注册新账号