如何在 Ruby on Rails 3.1 中禁用资产管道(链轮)消息的日志记录?

发布于 2024-11-15 01:56:46 字数 926 浏览 4 评论 0原文

默认情况下,在 Ruby on Rails 3.1 (RC1) 下,Sprockets 在(开发)日志中往往非常详细:

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)
...

我想降低详细级别或完全禁用它。

我假设有一种干净的方法可以通过在 environment.rbdevelopment.rb 中添加类似于 的配置行来禁用或减少日志记录的详细程度>config.active_record.logger = nil 静默 ActiveRecord SQL 语句。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

你怎么这么可爱啊 2024-11-22 01:56:46

将以下代码放入 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

更新:它现在也适用于 Ruby on Rails 3.2(之前的尝试修复了 before_dispatch,现在我们要根机架调用

更新:来自@macournoyer的适当的Rack中间件解决方案(而不是脆弱的alias_method_chainhttps://github.com/rails/rails/issues/2639#issuecomment-6591735< /a>

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

别挽留 2024-11-22 01:56:46

查看 https://github.com/evrone/quiet_assets 并将其包含到您的 Gem 中文件。

对于懒人:gem 'quiet_assets', group: :development

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

只怪假的太真实 2024-11-22 01:56:46

对于 Ruby on Rails 3.2,请将 config.assets.logger = false 添加到您的开发环境配置文件(通常位于 config/environments/development.rb)中。请参阅 #4512

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.

幽蝶幻影 2024-11-22 01:56:46

有两件事就足够了:

  1. config/enviroments/development.rb 中的config.assets.debug = false
  2. rake assets:precompile请参阅下面 @oma 的评论;这不是必需的

就这样!

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!

清风不识月 2024-11-22 01:56:46

最终,它将是 config.assets.logger = nil,但该部分目前已在 master 上存根(尚未完成)。

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

撩动你心 2024-11-22 01:56:46

我知道这是一个丑陋且临时的解决方案,但我使用这个:

tail -f 日志/development.log | grep -vE '资产'

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

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

伴我心暖 2024-11-22 01:56:46

许多人对 config.assets.logger = false 的使用感到困惑。以下是它的作用和不作用。

根据源文档

将 config.assets.logger 设置为 false 将关闭所提供的资产日志记录。

然而,这可能不是你想象的那样。 它仅禁用 sprocket“服务”日志,而不禁用 Ruby on Rails actionpack 请求日志。 Ruby on Rails 维护者在这里清楚地解释了这一点:
https://github.com/rails/rails/issues/4569#issuecomment- 3594500


以链接中的示例为例,这样的日志被禁用:

已提供资源 /jquery.isotope.js - 304 未修改(0ms)

但这样的日志不是

于 2012-01-20 23:16:46 -0500 开始获取 127.0.0.1 的“/assets/jquery.isotope.js?body=1”

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

七秒鱼° 2024-11-22 01:56:46
config.assets.quiet = true

这是最新的方法。

config.assets.quiet = true

This is the latest way to go.

新人笑 2024-11-22 01:56:46

config/environments 的文件 development.rb 中,您将找到行 config.assets.debug = true

将其切换为 false ,大部分资源加载输出将消失。在我的系统上,仅保留了 application.css 和 .js 两个请求。

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.

七秒鱼° 2024-11-22 01:56:46

使用:

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

这是相同的代码 添加了choonkeat。我刚刚将其包含在 Windows 下也可以工作。

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.

意中人 2024-11-22 01:56:46

在文件 config/environments/development.rb 中,请添加:

config.assets.debug = false

config.assets.logger = false

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

config.assets.debug = false

config.assets.logger = false
庆幸我还是我 2024-11-22 01:56:46

Lograge 获胜 - 它消除了 Ruby on Rails 烦人的默认记录器框的名称(例如记录资产、记录部分渲染),并且如果您想要添加/删除特定项目,则可以自定义。

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.

戈亓 2024-11-22 01:56:46

前面提到的链接解决方案有帮助:

https://github.com/evrone/quiet_assets

另外如下,它对我来说工作得很好:

3.1(仅)(3.2 在_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

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
倾城月光淡如水﹏ 2024-11-22 01:56:46

在 config/environments 中,将 config.log_level = :error 添加到要更改的 .rb 文件中。这会将日志设置更改为仅错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文