From 43af5cc7ec4f6ba314f9342e1172792060642a71 Mon Sep 17 00:00:00 2001 From: Andy Brody Date: Fri, 28 Apr 2017 01:45:18 -0400 Subject: [PATCH] WIP: Add option for a custom log proc. If `:log_proc` is passed, then rest-client will not output any log messages itself, but will instead call the proc with an event type and a hash of data that should be useful in outputting log messages. Folks can use this to implement log levels, redaction, etc. --- lib/restclient/request.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/restclient/request.rb b/lib/restclient/request.rb index 33ff87b..9687000 100644 --- a/lib/restclient/request.rb +++ b/lib/restclient/request.rb @@ -138,6 +138,8 @@ module RestClient end @log = args[:log] + @log_proc = args[:log_proc] + @tf = nil # If you are a raw request, this is your tempfile @max_redirects = args[:max_redirects] || 10 @processed_headers = make_headers headers @@ -540,6 +542,11 @@ module RestClient def log_request return unless log + if @log_proc + @log_proc.call(:request, {request: self, headers: processed_headers}) + return + end + out = [] out << "RestClient.#{method} #{redacted_url.inspect}" @@ -551,6 +558,11 @@ module RestClient def log_response res return unless log + if @log_proc + @log_proc.call(:request, {request: self, response: res}) + return + end + size = if @raw_response File.size(@tf.path) else @@ -790,6 +802,7 @@ module RestClient size += chunk.size if log if size == 0 + # TODO XXX log << "%s %s done (0 length file)\n" % [@method, @url] elsif total == 0 log << "%s %s (zero content length)\n" % [@method, @url]