Sha256: f2bc5a34f032a63b21343dc0853ea02632da398942c21693923ace60c22725da

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 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]
        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

12 entries across 12 versions & 1 rubygems

Version Path
ezlog-0.9.5 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.9.1 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.9.0 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.8.0 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.7.1 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.7.0 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.6.0 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.5.3 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.5.2 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.5.1 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.5.0 lib/ezlog/rails/active_record/log_subscriber.rb
ezlog-0.4.0 lib/ezlog/rails/active_record/log_subscriber.rb