Module: Callapi::Call::Request::Http::LogHelper

Included in:
Callapi::Call::Request::Http
Defined in:
lib/callapi/call/request/http/log_helper.rb

Instance Method Summary collapse

Instance Method Details

#with_loggingObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/callapi/call/request/http/log_helper.rb', line 4

def with_logging
  return yield if Callapi::Config.log_level == :none

  t0 = Time.now
  string = ''
  string << uri.host
  string << ":#{uri.port}" if uri.port

  puts "Sending request to #{string}".center(80, '-').colorize(:white).on_blue
  'PATH:    '.tap do |string|
    string << "#{request_method.to_s.upcase} "
    string << "#{uri.path}"
    puts string.colorize(:magenta)
  end
  'HEADERS: '.tap do |string|
    string << "#{headers}"
    puts string.colorize(:cyan)
  end
  'PARAMS:  '.tap do |string|
    string << "#{params}"
    puts string.colorize(:green)
  end

  response = yield

  response.tap do |response|
    "RESPONSE: [#{response.code}]\n".tap do |string|
      string << (response.body.nil? ? '[EMPTY BODY]' : response.body)
      puts string.colorize(:light_blue)
    end

    puts "request send (#{(Time.now - t0).round(3)} sec)".center(80, '-').colorize(:white).on_blue
  end
rescue StandardError => e
  puts "Exception occured, skipping logs".center(80, '-').colorize(:red).on_yellow
  raise e
end