Class: RailsSemanticLogger::Sequel::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/rails_semantic_logger/sequel/log_subscriber.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 10

def logger
  @logger
end

Class Method Details

.countObject



27
28
29
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 27

def self.count
  RequestLocals.fetch(:sql_count) { 0 }
end

.count=(value) ⇒ Object



23
24
25
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 23

def self.count= value
  RequestLocals.store[:sql_count] = value
end

.reset_countObject



37
38
39
40
41
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 37

def self.reset_count
  previous = count
  self.count = 0
  previous
end

.reset_runtimeObject



31
32
33
34
35
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 31

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

.runtimeObject



18
19
20
21
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 18

def self.runtime
  # ::ActiveRecord::RuntimeRegistry.sql_runtime ||= 0
  RequestLocals.fetch(:sql_runtime) { 0 }
end

.runtime=(value) ⇒ Object



13
14
15
16
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 13

def self.runtime= value
  # ::ActiveRecord::RuntimeRegistry.sql_runtime = value
  RequestLocals.store[:sql_runtime] = value
end

Instance Method Details

#sql(event) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_semantic_logger/sequel/log_subscriber.rb', line 43

def sql event
  self.class.runtime += event.duration
  self.class.count += 1
  return unless logger.debug?

  payload = event.payload
  name = payload[:name]

  log_payload = {sql: payload[:sql].squeeze(" ")}
  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 = {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