Class: Motion::HTTP::Logger

Inherits:
Object show all
Defined in:
lib/common/http/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled = true) ⇒ Logger



6
7
8
9
# File 'lib/common/http/logger.rb', line 6

def initialize(enabled = true)
  # TODO: add ability to configure amount of logging (i.e. request URL only, no body, etc)
  @enabled = enabled
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



4
5
6
# File 'lib/common/http/logger.rb', line 4

def enabled
  @enabled
end

Instance Method Details

#_loggerObject



19
20
21
# File 'lib/common/http/logger.rb', line 19

def _logger
  @_logger ||= Motion::Lager.new
end

#debug(message, color = :gray) ⇒ Object



23
24
25
# File 'lib/common/http/logger.rb', line 23

def debug(message, color = :gray)
  _logger.debug(message, color) if enabled
end

#disable!Object



15
16
17
# File 'lib/common/http/logger.rb', line 15

def disable!
  @enabled = false
end

#enable!Object



11
12
13
# File 'lib/common/http/logger.rb', line 11

def enable!
  @enabled = true
end

#error(message, color = :red) ⇒ Object



31
32
33
# File 'lib/common/http/logger.rb', line 31

def error(message, color = :red)
  _logger.error(message, color) # always log even if logging is disabled
end

#log(message, color = :white) ⇒ Object



27
28
29
# File 'lib/common/http/logger.rb', line 27

def log(message, color = :white)
  _logger.log(message, color) if enabled
end

#log_request(request) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/common/http/logger.rb', line 35

def log_request(request)
  debug "\nRequest:\n#{request.http_method.to_s.upcase} #{request.url}"
  request.headers.each do |k,v|
    debug "#{k}: #{v}"
  end
  debug(request.body) if request.body
end

#log_response(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/common/http/logger.rb', line 43

def log_response(response)
  debug "\nResponse:"
  if response.original_request
    debug "URL: #{response.original_request.url}"
  end
  debug "Status: #{response.status_code}"
  response.headers.each do |k,v|
    debug "#{k}: #{v}"
  end
  debug("\n#{response.body}")
end