Browse Source

backtrace_cleaning

main
pvincent 5 months ago
parent
commit
50af095c4e
  1. 2
      config/database.yml
  2. 2
      config/initializers/backtrace_cleaning.rb
  3. 22
      lib/formatters/ansi_formatter.rb
  4. 31
      lib/monkey_patches/active_record/log_subscriber.rb

2
config/database.yml

@ -6,7 +6,7 @@ default: &default
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: ct1.lxd
host: 127.0.0.1
development:
<<: *default

2
config/initializers/backtrace_cleaning.rb

@ -0,0 +1,2 @@
bc = Rails.backtrace_cleaner
bc.add_silencer { |line| %r{^lib/(monkey_patches|formatters)}.match?(line) }

22
lib/formatters/ansi_formatter.rb

@ -95,7 +95,19 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
def build_terminus = "#{origin} #{TERMINUS_STRING} "
def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
def stackisize(items)
return '' if items.empty?
traces = items.map do |item|
trace = item.match(/(.*:\d+):in `(.*)'/)
next item unless trace
m1, m2 = trace.captures
"#{m1} #{AnsiColors::BOLD}#{m2}#{AnsiColors::CLEAR}"
end
"\n#{traces.join("\n")}"
end
def build_dimensions(dimensions)
"#{origin} #{dimensions.rails} "
@ -117,17 +129,11 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
exc = log.exception
clazz = colorize("#{exc.class}\n", color_map[:fatal])
message = colorize(exc.message.chomp(''), color_map[:error])
backtrace = stackisize(clean_backtrace(exc.backtrace))
backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
"#{clazz}#{message}#{backtrace}"
end
def clean_backtrace(backtrace)
root_path = Rails.root.to_s
backtrace = backtrace.select { |line| line.starts_with?(root_path) }
backtrace.map { |line| line.delete_prefix("#{root_path}/") }
end
def level_char
case log.level
when :info, :debug then ' '

31
lib/monkey_patches/active_record/log_subscriber.rb

@ -1,31 +0,0 @@
# frozen_string_literal: true
module ActiveRecord
class LogSubscriber < ActiveSupport::LogSubscriber
def log_query_source
source = query_source_location
puts 'DEBUG logSubscriber monkeying'
return unless source
logger.debug("#{source}")
end
if Thread.respond_to?(:each_caller_location)
def query_source_location
Thread.each_caller_location do |location|
frame = backtrace_cleaner.clean_frame(location)
return frame if frame
end
nil
end
else
def query_source_location
backtrace_cleaner.clean(caller(1).lazy).first
end
end
end
end
ActiveRecord::LogSubscriber.attach_to :active_record
Loading…
Cancel
Save