Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 100 lines (87 sloc) 3.641 kb
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
1 module ActiveSupport
32de3b8 @mjankowski move example code to be above reconfiguring discussion; add clarity a…
mjankowski authored
2 # Backtraces often include many lines that are not relevant for the context under review. This makes it hard to find the
3 # signal amongst the backtrace noise, and adds debugging time. With a BacktraceCleaner, filters and silencers are used to
4 # remove the noisy lines, so that only the most relevant lines remain.
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
5 #
32de3b8 @mjankowski move example code to be above reconfiguring discussion; add clarity a…
mjankowski authored
6 # Filters are used to modify lines of data, while silencers are used to remove lines entirely. The typical filter use case
7 # is to remove lengthy path information from the start of each line, and view file paths relevant to the app directory
8 # instead of the file system root. The typical silencer use case is to exclude the output of a noisy library from the
9 # backtrace, so that you can focus on the rest.
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
10 #
9b96de6 @suchasurge Some style changes
suchasurge authored
11 # ==== Example:
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
12 #
13 # bc = BacktraceCleaner.new
eb39d0f @josh Use backtrace cleaner for dev mode exception page
josh authored
14 # bc.add_filter { |line| line.gsub(Rails.root, '') }
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
15 # bc.add_silencer { |line| line =~ /mongrel|rubygems/ }
16 # bc.clean(exception.backtrace) # will strip the Rails.root prefix and skip any lines from mongrel or rubygems
17 #
32de3b8 @mjankowski move example code to be above reconfiguring discussion; add clarity a…
mjankowski authored
18 # To reconfigure an existing BacktraceCleaner (like the default one in Rails) and show as much data as possible, you can
19 # always call <tt>BacktraceCleaner#remove_silencers!</tt>, which will restore the backtrace to a pristine state. If you
20 # need to reconfigure an existing BacktraceCleaner so that it does not filter or modify the paths of any lines of the
21 # backtrace, you can call BacktraceCleaner#remove_filters! These two methods will give you a completely untouched backtrace.
22 #
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
23 # Inspired by the Quiet Backtrace gem by Thoughtbot.
24 class BacktraceCleaner
25 def initialize
26 @filters, @silencers = [], []
27 end
eb39d0f @josh Use backtrace cleaner for dev mode exception page
josh authored
28
32de3b8 @mjankowski move example code to be above reconfiguring discussion; add clarity a…
mjankowski authored
29 # Returns the backtrace after all filters and silencers have been run against it. Filters run first, then silencers.
eb39d0f @josh Use backtrace cleaner for dev mode exception page
josh authored
30 def clean(backtrace, kind = :silent)
31 filtered = filter(backtrace)
32
33 case kind
34 when :silent
35 silence(filtered)
36 when :noise
37 noise(filtered)
38 else
39 filtered
40 end
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
41 end
42
43 # Adds a filter from the block provided. Each line in the backtrace will be mapped against this filter.
44 #
45 # Example:
46 #
47 # # Will turn "/my/rails/root/app/models/person.rb" into "/app/models/person.rb"
48 # backtrace_cleaner.add_filter { |line| line.gsub(Rails.root, '') }
49 def add_filter(&block)
50 @filters << block
51 end
52
32de3b8 @mjankowski move example code to be above reconfiguring discussion; add clarity a…
mjankowski authored
53 # Adds a silencer from the block provided. If the silencer returns true for a given line, it will be excluded from
54 # the clean backtrace.
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
55 #
56 # Example:
57 #
58 # # Will reject all lines that include the word "mongrel", like "/gems/mongrel/server.rb" or "/app/my_mongrel_server/rb"
59 # backtrace_cleaner.add_silencer { |line| line =~ /mongrel/ }
60 def add_silencer(&block)
61 @silencers << block
62 end
63
64 # Will remove all silencers, but leave in the filters. This is useful if your context of debugging suddenly expands as
32de3b8 @mjankowski move example code to be above reconfiguring discussion; add clarity a…
mjankowski authored
65 # you suspect a bug in one of the libraries you use.
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
66 def remove_silencers!
67 @silencers = []
68 end
69
dc97430 @dougalcorn ActiveSupport::BacktraceCleaner#remove_filters! allows for completely…
dougalcorn authored
70 def remove_filters!
71 @filters = []
72 end
73
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
74 private
75 def filter(backtrace)
76 @filters.each do |f|
77 backtrace = backtrace.map { |line| f.call(line) }
78 end
eb39d0f @josh Use backtrace cleaner for dev mode exception page
josh authored
79
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
80 backtrace
81 end
eb39d0f @josh Use backtrace cleaner for dev mode exception page
josh authored
82
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
83 def silence(backtrace)
84 @silencers.each do |s|
85 backtrace = backtrace.reject { |line| s.call(line) }
86 end
eb39d0f @josh Use backtrace cleaner for dev mode exception page
josh authored
87
88 backtrace
89 end
90
91 def noise(backtrace)
92 @silencers.each do |s|
93 backtrace = backtrace.select { |line| s.call(line) }
94 end
95
f42c77f @dhh Added ActiveSupport::BacktraceCleaner and Rails::BacktraceCleaner for…
dhh authored
96 backtrace
97 end
98 end
99 end
Something went wrong with that request. Please try again.