Rails 回溯消音器不起作用,而过滤器起作用 [英] Rails backtrace silencers do not work, while filters do

查看:54
本文介绍了Rails 回溯消音器不起作用,而过滤器起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的 Rails 4.2 设置.我想抑制长错误回溯.在下面的回溯日志中,第一行对我来说就足够了,接下来的 4 行可以删除

Fresh Rails 4.2 set up. I want to suppress long error backtraces. In the following backtrace log first line would be enough for me, and next 4 could be removed

ActionController::RoutingError (No route matches [GET] "/user"):
  actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'

我在 backtrace_silencers.rb

# config/initializers/backtrace_silencers.rb
Rails.backtrace_cleaner.add_silencer { |line| line =~ /lib/ }

服务器重启后 - 没有任何反应,我仍然收到相同的回溯日志.(应该删除与 /lib/ 匹配的每一行:http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.htm)

After server restart - nothing happens, i'm still getting same backtrace log. (Each line matching /lib/ was supposed to be removed: http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.htm)

健全性检查:

> line = "actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'"
=> "actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'"
> line =~ /lib/
=> 19

但过滤器有效:

# config/initializers/backtrace_silencers.rb
Rails.backtrace_cleaner.add_filter { |line| line.gsub('lib', 'yeah!') }

现在回溯看起来像这样:

Now backtrace looks like this:

ActionController::RoutingError (No route matches [GET] "/user"):
  actionpack (4.2.1) yeah!/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.1.2) yeah!/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) yeah!/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.1) yeah!/rails/rack/logger.rb:38:in `call_app'

希望得到如何使消音器工作的建议.干杯!

Would appreciate an advice how to make silencers work. Cheers!

推荐答案

问题出在 Minitest-Reporters gem(为 Minitest 输出添加颜色),它与 Rails 回溯过滤器混淆

Problem was with Minitest-Reporters gem (adds colors to Minitest output), it messes up with Rails backtrace filters

要修复您需要将以下内容添加到 test_helper.rb

To fix you need to add the following to test_helper.rb

Minitest::Reporters.use!(
  Minitest::Reporters::DefaultReporter.new,
  ENV,
  Minitest.backtrace_filter
)

此处描述的问题和解决方案详细信息:https://github.com/kern/minitest-reporters#注意事项

Problem and solution details described here: https://github.com/kern/minitest-reporters#caveats

这篇关于Rails 回溯消音器不起作用,而过滤器起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆