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.



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

def initialize(logger)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#generate_log_message(event) ⇒ Object



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

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



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

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