Class: ClientApiBuilder::ActiveSupportLogSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/client_api_builder/active_support_log_subscriber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ ActiveSupportLogSubscriber

Returns a new instance of ActiveSupportLogSubscriber.



10
11
12
# File 'lib/client_api_builder/active_support_log_subscriber.rb', line 10

def initialize(logger)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/client_api_builder/active_support_log_subscriber.rb', line 8

def logger
  @logger
end

Instance Method Details

#generate_log_message(event) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/client_api_builder/active_support_log_subscriber.rb', line 20

def generate_log_message(event)
  client = event.payload[:client]
  method = client.request_options[:method].to_s.upcase
  uri = client.request_options[:uri]
  response = client.response
  response_code = response ? response.code : 'UNKNOWN'

  "#{method} #{uri.scheme}://#{uri.host}#{uri.path}[#{response_code}] took #{event.duration.to_i}ms"
end

#subscribe!Object



14
15
16
17
18
# File 'lib/client_api_builder/active_support_log_subscriber.rb', line 14

def subscribe!
  ActiveSupport::Notifications.subscribe('client_api_builder.request') do |event|
    logger.info(generate_log_message(event))
  end
end