lib/smartsheet/api/request_logger.rb in smartsheet-1.0.0.beta.2 vs lib/smartsheet/api/request_logger.rb in smartsheet-1.0.0
- old
+ new
@@ -1,9 +1,10 @@
require 'logger'
module Smartsheet
module API
+ # Censors strings and hash values for select blacklisted keys
class Censor
EXPOSED_CHARS = 4
KEY_TO_STRING = ->(k){ k.to_s }
KEY_TO_DOWNCASE_STRING = ->(k){ k.to_s.downcase }
@@ -43,10 +44,11 @@
end
attr_reader :blacklist
end
+ # Logs request and response information, while censoring OAuth-relevant keys
class RequestLogger
QUERY_PARAM_CENSOR = Censor.new 'code', 'client_id', 'hash', 'refresh_token'
HEADER_CENSOR = Censor.new 'authorization'
PAYLOAD_CENSOR = Censor.new 'access_token', 'refresh_token'
@@ -78,15 +80,20 @@
log_status(Logger::INFO, response)
log_headers('Response', response)
log_body('Response', response.result)
end
- def log_error_response(request, error)
+ def log_api_error_response(request, error)
log_request_basics(Logger::ERROR, request)
log_api_error(Logger::ERROR, error)
end
+ def log_http_error_response(request, error)
+ log_request_basics(Logger::ERROR, request)
+ log_http_error(Logger::ERROR, error)
+ end
+
private
attr_reader :logger, :log_full_body
def log_request_basics(level, request)
@@ -110,10 +117,15 @@
"#{response.error_code}: #{response.message} - Ref ID: #{response.ref_id}"
end
log_headers('Response', response)
end
+ def log_http_error(level, response)
+ log_status(level, response)
+ log_headers('Response', response)
+ end
+
def log_status(level, response)
logger.log(level) { "Response: #{response.status_code} #{response.reason_phrase}" }
end
def log_headers(context, req_or_resp)
@@ -145,10 +157,11 @@
body_str
end
end
end
+ # Stubs all request logging methods by doing nothing (see {RequestLogger})
class MuteRequestLogger
def log_request(request)
end
def log_retry_attempt(request, response, attempt_num)
@@ -158,10 +171,13 @@
end
def log_successful_response(response)
end
- def log_error_response(request, error)
+ def log_api_error_response(request, error)
+ end
+
+ def log_http_error_response(request, error)
end
end
end
end
\ No newline at end of file