Sha256: b1938623361f95102410f9016ebf233284e512bb302d288397727a74e4dac8ef

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'request_store'

# Requiring this file, or calling `Gecko.enable_logging` will hook into the provided
# ActiveSupport::Notification hooks on requests and log ActiveRecord-style messages
# on API requests.
class GeckoLogSubscriber < ActiveSupport::LogSubscriber
  ENV_KEY = :"gecko-logger"

  def initialize
    super
    @odd = false
  end

  def request(event) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    RequestStore.store[ENV_KEY] = []
    payload = event.payload

    request_path = payload[:request_path]

    if payload[:params] && payload[:verb] == :get
      request_path = request_path + "?" + payload[:params].to_param
      RequestStore.store[ENV_KEY] << request_path
    end

    name  = "#{payload[:model_class]} Load (#{event.duration.round(1)}ms)"
    query = "#{payload[:verb].to_s.upcase} /#{request_path}"

    name = if odd?
             color(name, CYAN, true)
           else
             color(name, MAGENTA, true)
           end

    query = color(query, nil, true)

    debug("  #{name}  #{query}")
  end

  def odd?
    @odd = !@odd
  end
end

GeckoLogSubscriber.attach_to :gecko

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gecko-ruby-0.12.3 lib/gecko/ext/log_subscriber.rb
gecko-ruby-0.12.2 lib/gecko/ext/log_subscriber.rb
gecko-ruby-0.12.1 lib/gecko/ext/log_subscriber.rb
gecko-ruby-0.12.0 lib/gecko/ext/log_subscriber.rb
gecko-ruby-0.11.1 lib/gecko/ext/log_subscriber.rb
gecko-ruby-0.11.0 lib/gecko/ext/log_subscriber.rb
gecko-ruby-0.10.0 lib/gecko/ext/log_subscriber.rb