堆栈跟踪清理器
堆栈跟踪通常包含许多与正在审查的上下文无关的行。这使得在堆栈跟踪噪声中找到信号变得困难,并增加了调试时间。使用 BacktraceCleaner
,过滤器和静音器用于删除噪声行,以便仅保留最相关的行。
过滤器用于修改数据行,而静音器用于完全删除行。典型的过滤器用例是从每行的开头删除冗长的路径信息,并查看与应用程序目录相关的文件路径,而不是文件系统根目录。典型的静音器用例是将嘈杂库的输出从堆栈跟踪中排除,以便您可以专注于其他内容。
bc = ActiveSupport::BacktraceCleaner.new
bc.add_filter { |line| line.gsub(Rails.root.to_s, '') } # 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() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 34 def initialize @filters, @silencers = [], [] 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"
backtrace_cleaner.add_filter { |line| line.gsub(Rails.root.to_s, '') }
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 80 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) }
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 89 def add_silencer(&block) @silencers << block end
clean(backtrace, kind = :silent) 链接
在所有过滤器和静音器都运行后返回回溯。过滤器先运行,然后是静音器。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 43 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
。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 59 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
remove_filters!() 链接
删除所有过滤器,但保留静音器。如果您突然需要查看已过滤掉的回溯中的完整文件路径,这很有用。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 103 def remove_filters! @filters = [] end
remove_silencers!() 链接
移除所有静默器,但保留过滤器。如果您在调试时突然发现您使用的某个库存在错误,并且调试范围扩大,这将很有用。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 96 def remove_silencers! @silencers = [] end