class ActiveSupport::BacktraceCleaner
Inspired by the Quiet Backtrace gem by Thoughtbot.
backtrace, you can call BacktraceCleaner#remove_filters! These two methods will give you a completely untouched backtrace.
need to reconfigure an existing BacktraceCleaner so that it does not filter or modify the paths of any lines of the
always call BacktraceCleaner#remove_silencers!
, which will restore the backtrace to a pristine state. If you
To reconfigure an existing BacktraceCleaner (like the default one in Rails) and show as much data as possible, you can
bc.clean(exception.backtrace) # will strip the Rails.root prefix and skip any lines from mongrel or rubygems
bc.add_silencer { |line| line =~ /mongrel|rubygems/ }
bc.add_filter { |line| line.gsub(Rails.root, ”) }
bc = BacktraceCleaner.new
==== Example:
backtrace, so that you can focus on the rest.
instead of the file system root. The typical silencer use case is to exclude the output of a noisy library from the
is to remove lengthy path information from the start of each line, and view file paths relevant to the app directory
Filters are used to modify lines of data, while silencers are used to remove lines entirely. The typical filter use case
remove the noisy lines, so that only the most relevant lines remain.
signal amongst the backtrace noise, and adds debugging time. With a BacktraceCleaner, filters and silencers are used to
Backtraces often include many lines that are not relevant for the context under review. This makes it hard to find the
def add_filter(&block)
# Will turn "/my/rails/root/app/models/person.rb" into "/app/models/person.rb"
Example:
Adds a filter from the block provided. Each line in the backtrace will be mapped against this filter.
def add_filter(&block) @filters << block end
def add_silencer(&block)
# Will reject all lines that include the word "mongrel", like "/gems/mongrel/server.rb" or "/app/my_mongrel_server/rb"
Example:
the clean backtrace.
Adds a silencer from the block provided. If the silencer returns true for a given line, it will be excluded from
def add_silencer(&block) @silencers << block end
def clean(backtrace, kind = :silent)
def clean(backtrace, kind = :silent) filtered = filter(backtrace) case kind when :silent silence(filtered) when :noise noise(filtered) else filtered end end
def filter(backtrace)
def filter(backtrace) @filters.each do |f| backtrace = backtrace.map { |line| f.call(line) } end backtrace end
def initialize
def initialize @filters, @silencers = [], [] end
def noise(backtrace)
def noise(backtrace) @silencers.each do |s| backtrace = backtrace.select { |line| s.call(line) } end backtrace end
def remove_filters!
def remove_filters! @filters = [] end
def remove_silencers!
Will remove all silencers, but leave in the filters. This is useful if your context of debugging suddenly expands as
def remove_silencers! @silencers = [] end
def silence(backtrace)
def silence(backtrace) @silencers.each do |s| backtrace = backtrace.reject { |line| s.call(line) } end backtrace end