Sha256: 8edb3023b6c227d73033be187079ee40df82b5f0b8366b83419ffaf1e84bbfc4

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Lograge
  module Sql
    module Extension
      def extract_request(event, payload)
        super.merge!(extract_sql_queries)
      end

      def extract_sql_queries
        sql_queries = Thread.current[:lograge_sql_queries]
        return {} unless sql_queries

        Thread.current[:lograge_sql_queries] = nil
        { sql_queries: %('#{sql_queries.join("\n")}') }
      end
    end
  end
end

module Lograge
  class ActiveRecordLogSubscriber < ActiveSupport::LogSubscriber
    def sql(event)
      ActiveRecord::LogSubscriber.runtime += event.duration
      return if event.payload[:name] == 'SCHEMA'
      Thread.current[:lograge_sql_queries] ||= []
      Thread.current[:lograge_sql_queries] << ("#{event.payload[:name]} (#{event.duration.to_f.round(2)}) #{event.payload[:sql]}")
    end
  end
end

Lograge::RequestLogSubscriber.prepend Lograge::Sql::Extension

ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
  Lograge.unsubscribe(:active_record, subscriber) if subscriber.is_a?(ActiveRecord::LogSubscriber)
end

Lograge::ActiveRecordLogSubscriber.attach_to :active_record

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lograge-sql-0.1.5 lib/lograge/sql/extension.rb
lograge-sql-0.1.4 lib/lograge/sql/extension.rb
lograge-sql-0.1.3 lib/lograge/sql/extension.rb