11# frozen_string_literal: true
22
33module ActiveRecord
4- class LogSubscriber < ActiveSupport ::LogSubscriber # :nodoc:
4+ class LogSubscriber < ActiveSupport ::EventReporter :: LogSubscriber # :nodoc:
55 IGNORE_PAYLOAD_NAMES = [ "SCHEMA" , "EXPLAIN" ]
66
7+ self . namespace = "active_record"
8+
79 class_attribute :backtrace_cleaner , default : ActiveSupport ::BacktraceCleaner . new
810
911 def strict_loading_violation ( event )
1012 debug do
11- owner = event . payload [ :owner ]
12- reflection = event . payload [ :reflection ]
13- color ( reflection . strict_loading_violation_message ( owner ) , RED )
13+ owner = event [ :payload ] [ :owner ]
14+ klass = event [ :payload ] [ :class ]
15+ name = event [ :payload ] [ :name ]
16+ message = +"`#{ owner } ` is marked for strict_loading."
17+ message << " The #{ klass ? "#{ klass } association" : "polymorphic association" } "
18+ message << " named `:#{ name } ` cannot be lazily loaded."
19+
20+ color ( message , RED )
1421 end
1522 end
16- subscribe_log_level :strict_loading_violation , :debug
23+ event_log_level :strict_loading_violation , :debug
1724
1825 def sql ( event )
19- payload = event . payload
26+ payload = event [ : payload]
2027
2128 return if IGNORE_PAYLOAD_NAMES . include? ( payload [ :name ] )
2229
2330 name = if payload [ :async ]
24- "ASYNC #{ payload [ :name ] } (#{ payload [ :lock_wait ] . round ( 1 ) } ms) (db time #{ event . duration . round ( 1 ) } ms)"
31+ "ASYNC #{ payload [ :name ] } (#{ payload [ :lock_wait ] . round ( 1 ) } ms) (db time #{ payload [ :duration_ms ] . round ( 1 ) } ms)"
2532 else
26- "#{ payload [ :name ] } (#{ event . duration . round ( 1 ) } ms)"
33+ "#{ payload [ :name ] } (#{ payload [ :duration_ms ] . round ( 1 ) } ms)"
2734 end
2835 name = "CACHE #{ name } " if payload [ :cached ]
2936 sql = payload [ :sql ]
30- binds = nil
37+ binds = payload [ :binds ]
3138
3239 if payload [ :binds ] &.any?
33- casted_params = type_casted_binds ( payload [ :type_casted_binds ] )
34-
35- binds = [ ]
36- payload [ :binds ] . each_with_index do |attr , i |
37- attribute_name = if attr . respond_to? ( :name )
38- attr . name
39- elsif attr . respond_to? ( :[] ) && attr [ i ] . respond_to? ( :name )
40- attr [ i ] . name
41- else
42- nil
43- end
44-
45- filtered_params = filter ( attribute_name , casted_params [ i ] )
46-
47- binds << render_bind ( attr , filtered_params )
48- end
4940 binds = binds . inspect
5041 binds . prepend ( " " )
5142 end
@@ -55,7 +46,11 @@ def sql(event)
5546
5647 debug " #{ name } #{ sql } #{ binds } "
5748 end
58- subscribe_log_level :sql , :debug
49+ event_log_level :sql , :debug
50+
51+ def self . default_logger
52+ ActiveRecord ::Base . logger
53+ end
5954
6055 private
6156 def type_casted_binds ( casted_binds )
@@ -106,10 +101,6 @@ def sql_color(sql)
106101 end
107102 end
108103
109- def logger
110- ActiveRecord ::Base . logger
111- end
112-
113104 def debug ( progname = nil , &block )
114105 return unless super
115106
@@ -136,4 +127,6 @@ def filter(name, value)
136127 end
137128end
138129
139- ActiveRecord ::LogSubscriber . attach_to :active_record
130+ ActiveSupport . event_reporter . subscribe (
131+ ActiveRecord ::LogSubscriber . new , &ActiveRecord ::LogSubscriber . subscription_filter
132+ )
0 commit comments