module Lograge::Sql

Main gem module

Constants

VERSION

Gem version

Attributes

extract_event[RW]

Extract information from SQL event

formatter[RW]

Format SQL log

Public Class Methods

setup(config) click to toggle source

Initialise configuration with fallback to default values

# File lib/lograge/sql.rb, line 16
def setup(config)
  Lograge::Sql.formatter     = config.formatter     || default_formatter
  Lograge::Sql.extract_event = config.extract_event || default_extract_event

  # Disable existing ActiveRecord logging
  unless config.keep_default_active_record_log
    ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
      Lograge.unsubscribe(:active_record, subscriber) if subscriber.is_a?(ActiveRecord::LogSubscriber)
    end
  end

  return unless defined?(Lograge::ActiveRecordLogSubscriber)

  Lograge::ActiveRecordLogSubscriber.attach_to(:active_record)
end
store() click to toggle source

Gets the store, preferring RequestStore if the gem is found. @return [Hash, Thread] the RequestStore or the current Thread.

# File lib/lograge/sql.rb, line 34
def store
  defined?(RequestStore) ? RequestStore.store : Thread.current
end

Private Class Methods

default_extract_event() click to toggle source

By default, only extract values required for the default_formatter and already convert to a string

# File lib/lograge/sql.rb, line 49
def default_extract_event
  proc do |event|
    "#{event.payload[:name]} (#{event.duration.to_f.round(2)}) #{event.payload[:sql]}"
  end
end
default_formatter() click to toggle source

By default, the output is a concatenated string of all extracted events

# File lib/lograge/sql.rb, line 41
def default_formatter
  proc do |sql_queries|
    %('#{sql_queries.join("\n")}')
  end
end