14

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.

Flip
  • 6,233
  • 7
  • 46
  • 75
  • Hi - did you ever solve this issue? – garythegoat May 01 '15 at 02:57
  • 2
    Nope, sorry. Still hoping to get some answer here some time.. – Flip May 04 '15 at 15:05
  • 1
    Solved it yet? Have gone back and forth and into the Backtrace code and nothing works. It's filtering the output out correctly, but what's getting outputted to the console isn't Backtrace's filtered output! – parreirat Feb 19 '16 at 17:21
  • I noticed you are seeing this in a test. I'd search all code, including gems, for usage of the `remove_silencers!` method from `BacktraceCleaner` class. Perhaps it is called somewhere and all the silencers are wiped out when running your tests. – Matouš Borák Mar 05 '16 at 15:10
  • Also, this SO answer might be related if you are using minitest: http://stackoverflow.com/a/29437544/1544012. – Matouš Borák Mar 05 '16 at 15:16

2 Answers2

6

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
Mikhail Chuprynski
  • 2,404
  • 2
  • 29
  • 42
-2

Your logs show /.rvm/ your setting /rvm/ - can this be the cause? Give it a try and dont forget to stop / restart the server.