Class: RailsSemanticLogger::ActiveRecord::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber show all
Defined in:
lib/rails_semantic_logger/active_record/log_subscriber.rb

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
%w[SCHEMA EXPLAIN].freeze
RAILS_VERSION_ENDING_SET_RUNTIME_SUPPORT =
Gem::Version.new( "8.0.3")

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveSupport::LogSubscriber

#silenced?

Class Attribute Details

.loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/rails_semantic_logger/active_record/log_subscriber.rb', line 8

def logger
  @logger
end

Class Method Details

.reset_runtimeObject



25
26
27
28
29
# File 'lib/rails_semantic_logger/active_record/log_subscriber.rb', line 25

def self.reset_runtime
  rt           = runtime
  self.runtime = 0
  rt
end

.runtimeObject



19
20
21
22
23
# File 'lib/rails_semantic_logger/active_record/log_subscriber.rb', line 19

def self.runtime
  ::ActiveRecord::RuntimeRegistry.respond_to?(:stats) ?
    ::ActiveRecord::RuntimeRegistry.stats.sql_runtime ||= 0 :
    ::ActiveRecord::RuntimeRegistry.sql_runtime ||= 0
end

.runtime=(value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rails_semantic_logger/active_record/log_subscriber.rb', line 11

def self.runtime=(value)
  return if Rails.version >= RAILS_VERSION_ENDING_SET_RUNTIME_SUPPORT

  ::ActiveRecord::RuntimeRegistry.respond_to?(:stats) ?
    ::ActiveRecord::RuntimeRegistry.stats.sql_runtime = value :
    ::ActiveRecord::RuntimeRegistry.sql_runtime = value
end

Instance Method Details

#sql(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rails_semantic_logger/active_record/log_subscriber.rb', line 31

def sql(event)
  self.class.runtime += event.duration
  return unless logger.debug?

  payload = event.payload
  name    = payload[:name]
  return if IGNORE_PAYLOAD_NAMES.include?(name)

  log_payload         = {sql: payload[:sql]}
  log_payload[:binds] = bind_values(payload) unless (payload[:binds] || []).empty?
  log_payload[:allocations] = event.allocations if event.respond_to?(:allocations)
  log_payload[:cached] = event.payload[:cached]
  log_payload[:async] = true if event.payload[:async]

  log = {
    message:  name,
    payload:  log_payload,
    duration: event.duration
  }

  # Log the location of the query itself.
  if logger.send(:level_index) >= SemanticLogger.backtrace_level_index
    log[:backtrace] = SemanticLogger::Utils.strip_backtrace(caller)
  end

  logger.debug(log)
end