Browse Source

logSubscriber monkeypatch

main
pvincent 7 months ago
parent
commit
4df03d2247
  1. 12
      lib/formatters/ansi_formatter.rb
  2. 31
      lib/monkey_patches/active_record/log_subscriber.rb

12
lib/formatters/ansi_formatter.rb

@ -26,10 +26,10 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
))
@memory = nil
puts 'INITIALIZED2'
end
def call(log, logger)
# puts caller_locations(1, 15)
log = alter(log)
self.log = log
@ -40,8 +40,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
end
def reject(log)
log.name == 'ScoresController' || false
false
true if log.name == 'ActionView::Base' && log.message.starts_with?(' Rendering')
end
private
@ -49,6 +48,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
def alter(log)
if log.name == 'Rails'
if log.message
log.message.lstrip!
log.message.chomp!('')
if log.message.starts_with?('Started')
rails = '╓─╖'
@ -68,6 +68,8 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
elsif log.message.starts_with?('Completed 5')
log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
elsif log.message =~ /^(Processing|Parameters)/
log.level = :debug
end
elsif log.exception
log.dimensions = AnsiDimensions.new(
@ -76,7 +78,11 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
terminus: true
)
end
elsif log.name =~ /^(ActionView|ActiveRecord)::Base/
log.level = :debug
log.message.lstrip!
end
log
end

31
lib/monkey_patches/active_record/log_subscriber.rb

@ -0,0 +1,31 @@
# 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