Module: Chalk::Log::Utils
- Defined in:
- lib/chalk-log/utils.rb
Class Method Summary collapse
-
.compress_backtrace(backtrace) ⇒ Object
Compresses a backtrace, omitting gem lines (unless they appear before any application lines).
-
.explode_nested_hash(hash, prefix = nil) ⇒ Object
Explodes a nested hash to just have top-level keys.
-
.format_backtrace(backtrace) ⇒ Object
Nicely formats a backtrace:.
Class Method Details
.compress_backtrace(backtrace) ⇒ Object
Compresses a backtrace, omitting gem lines (unless they appear before any application lines).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/chalk-log/utils.rb', line 48 def self.compress_backtrace(backtrace) compressed = [] gemdir = Gem.dir hit_application = false # This isn't currently read by anything, but we could easily use # it to limit the number of leading gem lines. leading_lines = 0 gemlines = 0 backtrace.each do |line| if line.start_with?(gemdir) # If we're in a gem, always increment the counter. Record the # first three lines if we haven't seen any application lines # yet. if !hit_application compressed << line leading_lines += 1 else gemlines += 1 end elsif gemlines > 0 # If we were in a gem and now are not, record the number of # lines skipped. compressed << "<#{gemlines} #{gemlines == 1 ? 'line' : 'lines'} omitted>" compressed << line hit_application = true gemlines = 0 else # If we're in the application, always record the line. compressed << line hit_application = true end end compressed end |
.explode_nested_hash(hash, prefix = nil) ⇒ Object
Explodes a nested hash to just have top-level keys. This is generally useful if you have something that knows how to parse kv-pairs.
“‘ruby explode_nested_hash(foo: ’baz’, bat: ‘zom’) #=> => ‘baz’, ‘foo_bat’ => ‘zom’ “‘
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/chalk-log/utils.rb', line 30 def self.explode_nested_hash(hash, prefix=nil) exploded = {} hash.each do |key, value| new_prefix = prefix ? "#{prefix}_#{key}" : key.to_s if value.is_a?(Hash) exploded.merge!(self.explode_nested_hash(value, new_prefix)) else exploded[new_prefix] = value end end exploded end |
.format_backtrace(backtrace) ⇒ Object
Nicely formats a backtrace:
“‘ruby format_backtrace([’line1’, ‘line2’]) #=> line1
line2
“‘
(Used internally when ‘Chalk::Log` is formatting exceptions.)
TODO: add autotruncating of backtraces.
13 14 15 16 17 18 19 20 |
# File 'lib/chalk-log/utils.rb', line 13 def self.format_backtrace(backtrace) if configatron.chalk.log.compress_backtraces backtrace = compress_backtrace(backtrace) backtrace << '(Disable backtrace compression by setting configatron.chalk.log.compress_backtraces = false.)' end " " + backtrace.join("\n ") end |