跳到內容 跳到搜尋

後端追蹤清理程式

後端追蹤常包含許多與正在檢視的內容無關的程式行。這使得難以在後端追蹤雜訊中找出訊號,還增加了除錯時間。透過 BacktraceCleaner,使用過濾器和靜音器移除產生雜訊的程式行,僅保留最相關的程式行。

過濾器用於修改資料行,而靜音器用於完全移除程式行。常見的過濾器使用案例是移除每行開頭處冗長的路徑資訊,並檢視與應用程式目錄相關的檔案路徑,而非檔案系統根目錄。常見的靜音器使用案例是從後端追蹤中排除雜訊程式庫的輸出,以便專注於其他部分。

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 程式集。

方法
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.start_with?(root) ? line.from(root.size) : line }
# File activesupport/lib/active_support/backtrace_cleaner.rb, line 83
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 92
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)

傳回套用所有篩選器的 frame。如果 frame 已設定為靜音,則傳回 nil

# File activesupport/lib/active_support/backtrace_cleaner.rb, line 61
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

filter(backtrace, kind = :silent)

別名為:clean

remove_filters!()

移除所有篩選器,但保留靜音。當您突然想要在 backtrace 中查看您已過濾掉的完整檔案路徑時會很有用。

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

remove_silencers!()

移除所有靜音,但保留篩選器。當您懷疑所使用的其中一個函式庫有錯誤,因此突然擴大偵錯範圍時會很有用。

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