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 ⇒ Logger
constructor
A new instance of Logger.
- #log(message, color = :white) ⇒ Object
- #log_request(request) ⇒ Object
- #log_response(response) ⇒ Object
Constructor Details
#initialize ⇒ Logger
Returns a new instance of Logger.
6 7 8 |
# File 'lib/common/http/logger.rb', line 6 def initialize @enabled = false # logging is disabled by default 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
18 19 20 |
# File 'lib/common/http/logger.rb', line 18 def _logger @_logger ||= Motion::Lager.new end |
#debug(message, color = :gray) ⇒ Object
22 23 24 |
# File 'lib/common/http/logger.rb', line 22 def debug(, color = :gray) _logger.debug(, color) if enabled end |
#disable! ⇒ Object
14 15 16 |
# File 'lib/common/http/logger.rb', line 14 def disable! @enabled = false end |
#enable! ⇒ Object
10 11 12 |
# File 'lib/common/http/logger.rb', line 10 def enable! @enabled = true end |
#error(message, color = :red) ⇒ Object
30 31 32 |
# File 'lib/common/http/logger.rb', line 30 def error(, color = :red) _logger.error(, color) # always log even if logging is disabled end |
#log(message, color = :white) ⇒ Object
26 27 28 |
# File 'lib/common/http/logger.rb', line 26 def log(, color = :white) _logger.log(, color) if enabled end |
#log_request(request) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/common/http/logger.rb', line 34 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
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/common/http/logger.rb', line 42 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 |