Class: ApiClient::Connection::Middlewares::Request::Logger

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/api_client/connection/middlewares/request/logger.rb

Defined Under Namespace

Classes: CurrentTimestamp

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = nil) ⇒ Logger

Returns a new instance of Logger.



26
27
28
29
# File 'lib/api_client/connection/middlewares/request/logger.rb', line 26

def initialize(app, logger = nil)
  @logger = logger || ::Logger.new(STDOUT)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/api_client/connection/middlewares/request/logger.rb', line 4

def call(env)
  debug_lines = []
  should_log_details = @logger.level <= ::Logger::DEBUG

  gather_request_debug_lines(env, debug_lines) if should_log_details

  start = CurrentTimestamp.milis
  response = @app.call(env)
  taken_sec = (CurrentTimestamp.milis - start) / 1000.0

  gather_response_debug_lines(response, taken_sec, debug_lines) if response && should_log_details

  if should_log_details
    debug_lines.each { |line| line.encode!("UTF-8", invalid: :replace, undef: :replace) }
    @logger.debug { debug_lines.join("\n") }
  else
    @logger.info { "#{env[:method].to_s.upcase} #{env[:url]}: #{"%.4f" % taken_sec} seconds" }
  end

  response
end