Browse Source

logSubscriber monkeypatch

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