Class: Motion::HTTP::Logger
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
Instance Method Summary collapse
- #_logger ⇒ Object
- #debug(message, color = :gray) ⇒ Object
- #disable! ⇒ Object
- #enable! ⇒ Object
- #error(message, color = :red) ⇒ Object
-
#initialize(enabled = true) ⇒ Logger
constructor
A new instance of Logger.
- #log(message, color = :white) ⇒ Object
- #log_request(request) ⇒ Object
- #log_response(response) ⇒ Object
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
#enabled ⇒ Object (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
#_logger ⇒ Object
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(, color = :gray) _logger.debug(, 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(, color = :red) _logger.error(, 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(, color = :white) _logger.log(, 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 |