Sha256: be87820e7176cfbb3a5d1c009b2ea9a2b479b33314e420d842567fec9db0cba8

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Lograge
  module Sql
    module Extension
      def extract_request(event, payload)
        super.merge!(extract_sql_queries(payload[:uuid], payload[:log_sql]))
      end

      def extract_sql_queries(uuid, log_sql)
        sql_queries = Thread.current[:lograge_sql_queries]
        return {} unless sql_queries
        return {} unless log_sql

        Thread.current[:lograge_sql_queries] = nil

        formatted_queries = if uuid
          queries_with_uuid = sql_queries.map do |query|
            "\n[#{uuid}] #{query}"
          end

          queries_with_uuid.join
        else
          sql_queries.join("\n")
        end

        { sql_queries: formatted_queries }
      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

2 entries across 2 versions & 2 rubygems

Version Path
tagged-lograge-sql-0.1.7 lib/lograge/sql/extension.rb
lograge-sql-one-line-0.1.7 lib/lograge/sql/extension.rb