如何在 Ruby on Rails 3.1 中禁用资产管道(链轮)消息的日志记录?
默认情况下,在 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.rb
或 development.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
将以下代码放入
config/initializers/quiet_assets.rb
更新:它现在也适用于 Ruby on Rails 3.2(之前的尝试修复了
before_dispatch
,现在我们要根机架调用
)更新:来自@macournoyer的适当的Rack中间件解决方案(而不是脆弱的
alias_method_chain
)https://github.com/rails/rails/issues/2639#issuecomment-6591735< /a>Place the following code in
config/initializers/quiet_assets.rb
Updated: It now works for Ruby on Rails 3.2 too (previous attempt fixes
before_dispatch
, and now we're going for the root rackcall
instead)Update: A proper Rack middleware solution (instead of fragile
alias_method_chain
) from @macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735查看 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
对于 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 atconfig/environments/development.rb
. See #4512.有两件事就足够了:
config/enviroments/development.rb
中的config.assets.debug = false
rake assets:precompile
。 请参阅下面 @oma 的评论;这不是必需的就这样!
Two things are enough:
config.assets.debug = false
inconfig/enviroments/development.rb
rake assets:precompile
. See comment by @oma below; this is not neededThat's all!
最终,它将是 config.assets.logger = nil,但该部分目前已在 master 上存根(尚未完成)。
Eventually, it will be
config.assets.logger = nil
, but that part is currently stubbed on master (not done yet).我知道这是一个丑陋且临时的解决方案,但我使用这个:
I know it's an ugly and temporary solution, but I use this:
许多人对 config.assets.logger = false 的使用感到困惑。以下是它的作用和不作用。
根据源文档:
然而,这可能不是你想象的那样。 它仅禁用 sprocket“服务”日志,而不禁用 Ruby on Rails actionpack 请求日志。 Ruby on Rails 维护者在这里清楚地解释了这一点:
https://github.com/rails/rails/issues/4569#issuecomment- 3594500
以链接中的示例为例,这样的日志被禁用:
但这样的日志不是
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:
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:
But logs like this are not
这是最新的方法。
This is the latest way to go.
在 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.使用:
这是相同的代码 添加了choonkeat。我刚刚将其包含在 Windows 下也可以工作。
Use:
It's the same code choonkeat added. I just included it to work under Windows as well.
在文件 config/environments/development.rb 中,请添加:
In file config/environments/development.rb please add:
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.
前面提到的链接解决方案有帮助:
https://github.com/evrone/quiet_assets
另外如下,它对我来说工作得很好:
3.1(仅)(3.2 在_dipatch 之前中断)
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)
在 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.