Created
December 27, 2011 13:04
-
-
Save okitan/1523606 to your computer and use it in GitHub Desktop.
Rails formatted backtrace on logger (tested in Rails 3.0.10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.backtrace_cleaner.remove_silencers! | |
Rails.backtrace_cleaner.add_silencer { |line| line =~ /spec/ } | |
module ActiveSupport | |
class BacktraceCleaner | |
def clean_with_format(backtrace, kind = :silent) | |
filtered = clean_without_format(backtrace, kind) | |
splits = filtered.map do |line| | |
gem, version, line, method = line.split | |
gem, version, line, method = '', '', gem, version unless line | |
[ gem, version, [ line, method ].join(' ') ] | |
end | |
lengths = splits.map do |gem, version, place| | |
[ gem.length, version.length, 0 ] | |
end.transpose.map(&:max) | |
splits.map do |items| | |
items.zip(lengths).map do |item, length| | |
item.ljust(length) | |
end.join(' ') | |
end | |
end | |
alias_method_chain :clean, :format | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveSupport | |
class BufferedLogger | |
def error_with_backtrace(e) | |
if e.is_a?(Exception) | |
[ e.to_s, Rails.backtrace_cleaner.clean(e.backtrace) ].join("\n") | |
else | |
e | |
end | |
end | |
def fatal_with_backtrace(message = nil, progname = nil, &block) | |
fatal_without_backtrace(error_with_backtrace(message), progname, &block) | |
end | |
alias_method_chain :fatal, :backtrace | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment