Sha256: 553706d88574593d9ac03aa24967f71d64e4d088d17bbf6cf4fe66294b79ac53

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

module Ezlog
  module Rails
    module ActiveRecord
      class LogSubscriber < ::ActiveSupport::LogSubscriber
        def sql(event)
          ::ActiveRecord::Base.logger.debug log_message_from(event)
        end

        private

        def log_message_from(event)
          basic_message_from(event).tap do |message|
            params = params_from event
            message[:params] = params if params.any?
          end
        end

        def basic_message_from(event)
          {
            message: "SQL - #{event.payload[:name]} (#{event.duration.round(3)}ms)",
            sql: event.payload[:sql],
            duration_sec: (event.duration / 1000.0).round(5)
          }
        end

        def params_from(event)
          return {} if event.payload.fetch(:binds, []).empty?

          params = event.payload[:binds]
          values = type_casted_values_from event
          param_value_pairs = params.zip(values).map do |param, value|
            [param.name, value_of(param, value)]
          end

          Hash[param_value_pairs]
        rescue NoMethodError
          params
        end

        def type_casted_values_from(event)
          binds = event.payload[:type_casted_binds]
          binds.respond_to?(:call) ? binds.call : binds
        end

        def value_of(param, value)
          param.type.binary? ? '-binary data-' : value
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ezlog-0.10.7 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.10.5 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.10.3 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.10.2 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.10.1 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.10.0 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.9.6 lib/ezlog/rails/active_record/log_subscriber.rb