Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backtrace Silencer not working

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.

like image 261
Flip Avatar asked Mar 03 '15 16:03

Flip


1 Answers

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
like image 69
Mikhail Chuprynski Avatar answered Nov 02 '22 22:11

Mikhail Chuprynski