回溯消音器不工作 [英] Backtrace Silencer not working

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

问题描述

在我的 Rails 应用程序中,我设置了以下回溯消音器,正如 Michael Hartl 在他的 Rails 教程中所建议的:

Rails.backtrace_cleaner.add_silencer { |line|行 =~/rvm/}

但我仍然得到了我打算过滤掉的所有噪音:

7:13:55 - INFO - 运行:test/controllers/tags_controller_test.rb开始错误[test_should_get_index",标签控制器测试,0.45206]test_should_get_index#TagsControllerTest (0.45s)ActionController::UrlGenerationError:ActionController::UrlGenerationError: 没有路由匹配 {:action=>"index", :controller=>"tags"}/Users/chris/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.1.6/lib/action_dispatch/journey/formatter.rb:39:in `generate'/Users/chris/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.1.6/lib/action_dispatch/routing/route_set.rb:599:in`generate'

很明显,字符串rvm"出现在最后两行中.但他们仍然出现.将字符串更改为.rvm"没有任何区别.

解决方案

这是因为 https://github.com/vipulnsward/rails/blob/ecc8f283cfc1b002b5141c527a827e74b770f2f0/actionpack/lib/action_dispatch/lib/action_dispatch#16L5/alib.//p>

由于 application_trace 为空(这是因为错误不是来自用户代码而是路由错误),我们将回退到 framework_trace,它不会对其进行过滤(它只过滤噪音).

您可以通过创建自己的 log_formatter 来解决它.在你的 development.rb 和/或 test.rb 添加

config.log_formatter = SilentLogger.newconfig.log_formatter.add_silencer { |行|行 =~/rvm/}

并在模型中创建简单的类,只需要call 方法.在那里您可以根据需要修改回溯.

class SilentLogger定义初始化@silencers = []结尾def add_silencer(&block)@消音器<<堵塞结尾def call(severity, timestamp, progname, msg)回溯 = (String === msg) ?"#{msg}\n" : "#{msg.inspect}\n"如果@silencers.empty 返回回溯?@silencers.each 做 |s|backtrace = backtrace.split("\n").delete_if { |line|s.call(line) }结尾backtrace.join("\n")结尾结尾

In my Rails app, I have set up the following backtrace silencer, as suggested by Michael Hartl in his Rails tutorial:

Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }

But still I get all the noise I intended to filter out:

7:13:55 - INFO - Running: test/controllers/tags_controller_test.rb
Started

ERROR["test_should_get_index", TagsControllerTest, 0.45206]
test_should_get_index#TagsControllerTest (0.45s)
ActionController::UrlGenerationError:               
ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"tags"}
        /Users/chris/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.1.6/lib/action_dispatch/journey/formatter.rb:39:in `generate'
        /Users/chris/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.1.6/lib/action_dispatch/routing/route_set.rb:599:in `generate'

Clearly the string "rvm" is present in the last two lines. But still they show up. Changing the string to ".rvm" didn't make any difference.

This is because of https://github.com/vipulnsward/rails/blob/ecc8f283cfc1b002b5141c527a827e74b770f2f0/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L155-L156

Since application_trace is empty(This is because error is not from user code but route error), we are falling back to framework_trace, which does not filter it (it filters only noise).

You can solve it with creating your own log_formatter. In your development.rb and/or test.rb add

config.log_formatter = SilentLogger.new
config.log_formatter.add_silencer { |line| line =~ /rvm/ }

And create simple class in models with only method call required. There you can modify your backtrace as you wish.

class SilentLogger
  def initialize
    @silencers = []
  end

  def add_silencer(&block)
    @silencers << block
  end

  def call(severity, timestamp, progname, msg)
    backtrace = (String === msg) ? "#{msg}\n" : "#{msg.inspect}\n"

    return backtrace if @silencers.empty?

    @silencers.each do |s|
      backtrace = backtrace.split("\n").delete_if { |line| s.call(line) }
    end

    backtrace.join("\n")
  end
end

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

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