Class: HTTParty::Logger::CurlLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/logger/curl_logger.rb

Overview

:nodoc:

Constant Summary

TAG_NAME =
HTTParty.name
OUT =
">"
IN =
"<"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, level) ⇒ CurlLogger



10
11
12
13
# File 'lib/httparty/logger/curl_logger.rb', line 10

def initialize(logger, level)
  @logger = logger
  @level  = level.to_sym
end

Instance Attribute Details

#current_timeObject

Returns the value of attribute current_time



8
9
10
# File 'lib/httparty/logger/curl_logger.rb', line 8

def current_time
  @current_time
end

#levelObject

Returns the value of attribute level



8
9
10
# File 'lib/httparty/logger/curl_logger.rb', line 8

def level
  @level
end

#loggerObject

Returns the value of attribute logger



8
9
10
# File 'lib/httparty/logger/curl_logger.rb', line 8

def logger
  @logger
end

Instance Method Details

#format(request, response) ⇒ Object



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
41
# File 'lib/httparty/logger/curl_logger.rb', line 15

def format(request, response)
  messages        = []
  time            = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
  http_method     = request.http_method.name.split("::").last.upcase
  path            = request.path.to_s

  messages << print(time, OUT, "#{http_method} #{path}")

  if request.options[:headers] && request.options[:headers].size > 0
    request.options[:headers].each do |k, v|
      messages << print(time, OUT, "#{k}: #{v}")
    end
  end

  messages << print(time, OUT, request.raw_body)
  messages << print(time, OUT, "")
  messages << print(time, IN, "HTTP/#{response.http_version} #{response.code}")

  headers = response.respond_to?(:headers) ? response.headers : response
  response.each_header do |response_header|
    messages << print(time, IN, "#{response_header.capitalize}: #{headers[response_header]}")
  end

  messages << print(time, IN, "\n#{response.body}")

  @logger.send @level, messages.join("\n")
end


43
44
45
# File 'lib/httparty/logger/curl_logger.rb', line 43

def print(time, direction, line)
  "[#{TAG_NAME}] [#{time}] #{direction} #{line}"
end