跳至内容 跳至搜索

回溯清理器

回溯(Backtraces)通常包含许多与当前审查上下文无关的行。这使得难以在回溯噪音中找到有用的信号,并增加了调试时间。使用 BacktraceCleaner,可以通过过滤器(filters)和静音器(silencers)来移除这些噪音行,从而只保留最相关的行。

过滤器用于修改数据行,而静音器用于完全移除行。典型的过滤器用例是移除每行开头冗长的路径信息,并显示与应用程序目录相关的路径,而不是文件系统根目录。典型的静音器用例是排除嘈杂库的输出,以便您可以专注于其他部分。

bc = ActiveSupport::BacktraceCleaner.new
root = "#{Rails.root}/"
bc.add_filter   { |line| line.delete_prefix(root) } # strip the Rails.root prefix
bc.add_silencer { |line| /puma|rubygems/.match?(line) } # skip any lines from puma or rubygems
bc.clean(exception.backtrace) # perform the cleanup

要重新配置现有的 BacktraceCleaner(例如 Rails 中的默认实例)并显示尽可能多的数据,您可以随时调用 BacktraceCleaner#remove_silencers!,这将使回溯恢复到原始状态。如果您需要重新配置现有的 BacktraceCleaner,使其不过滤或修改回溯的任何行路径,您可以调用 BacktraceCleaner#remove_filters!。这两个方法将为您提供一个完全未修改的回溯。

灵感来自 thoughtbot 的 Quiet Backtrace gem。

方法
A
C
F
N
R

常量

FORMATTED_GEMS_PATTERN = /\A[^\/]+ \([\w.]+\) /
 

类公共方法

new()

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 35
def initialize
  @filters, @silencers = [], []
  add_core_silencer
  add_gem_filter
  add_gem_silencer
  add_stdlib_silencer
end

实例公共方法

add_filter(&block)

从提供的块中添加一个过滤器。回溯中的每一行都将与此过滤器进行匹配。

# Will turn "/my/rails/root/app/models/person.rb" into "app/models/person.rb"
root = "#{Rails.root}/"
backtrace_cleaner.add_filter { |line| line.delete_prefix(root) }
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 154
def add_filter(&block)
  @filters << block
end

add_silencer(&block)

从提供的块中添加一个静音器。如果静音器对给定行返回 true,则该行将被排除在清理后的回溯之外。

# Will reject all lines that include the word "puma", like "/gems/puma/server.rb" or "/app/my_puma_server/rb"
backtrace_cleaner.add_silencer { |line| /puma/.match?(line) }
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 163
def add_silencer(&block)
  @silencers << block
end

clean(backtrace, kind = :silent)

在所有过滤器和静音器都运行完成后返回清理后的回溯。过滤器先运行,然后是静音器。

也别名为: filter
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 45
def clean(backtrace, kind = :silent)
  filtered = filter_backtrace(backtrace)

  case kind
  when :silent
    silence(filtered)
  when :noise
    noise(filtered)
  else
    filtered
  end
end

clean_frame(frame, kind = :silent)

返回应用了所有过滤器后的帧。如果帧被静默,则返回 nil

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 73
def clean_frame(frame, kind = :silent)
  frame = frame.to_s
  @filters.each do |f|
    frame = f.call(frame.to_s)
  end

  case kind
  when :silent
    frame unless @silencers.any? { |s| s.call(frame) }
  when :noise
    frame if @silencers.any? { |s| s.call(frame) }
  else
    frame
  end
end

clean_locations(locations, kind = :silent)

给定一个 Thread::Backtrace::Location 对象数组,返回一个包含清理后的对象的数组。

clean_locations = backtrace_cleaner.clean_locations(caller_locations)

过滤器和静音器像往常一样接收字符串。但是,返回数组中位置的 path 属性是原始的、未过滤的,因为位置是不可变的。

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 67
def clean_locations(locations, kind = :silent)
  locations.select { |location| clean_frame(location, kind) }
end

filter(backtrace, kind = :silent)

别名为: clean

first_clean_frame(kind = :silent)

返回调用者回溯的第一个清理后的帧,如果不存在则返回 nil

帧是字符串。

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 94
def first_clean_frame(kind = :silent)
  caller_location_skipped = false

  Thread.each_caller_location do |location|
    unless caller_location_skipped
      caller_location_skipped = true
      next
    end

    frame = clean_frame(location, kind)
    return frame if frame
  end
end

first_clean_location(kind = :silent)

返回调用者调用堆栈的第一个清理后的位置,如果不存在则返回 nil

位置是 Thread::Backtrace::Location 对象。由于它们是不可变的,因此它们的 path 属性是原始的,但过滤器会在内部应用,因此静音器仍然可以依赖它们。

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 113
def first_clean_location(kind = :silent)
  caller_location_skipped = false

  Thread.each_caller_location do |location|
    unless caller_location_skipped
      caller_location_skipped = true
      next
    end

    return location if clean_frame(location, kind)
  end
end

remove_filters!()

移除所有过滤器,但保留静音器。如果您突然需要查看回溯中已过滤掉的所有文件路径,此方法非常有用。

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 177
def remove_filters!
  @filters = []
end

remove_silencers!()

移除所有静音器,但保留过滤器。如果您怀疑所使用的某个库存在 bug,并且调试范围突然扩大,此方法非常有用。

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 170
def remove_silencers!
  @silencers = []
end