From 1d7315ee9bec3d9528da171d92fdcd9fa92f4b62 Mon Sep 17 00:00:00 2001 From: Steven Bazyl Date: Fri, 4 Jan 2013 16:14:14 -0800 Subject: [PATCH] Sprinkle of logging --- lib/google/api_client.rb | 8 ++++++-- lib/google/api_client/logging.rb | 24 +++++++++++++----------- lib/google/api_client/request.rb | 10 +++++++++- lib/google/api_client/version.rb | 2 +- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index deeed6201..bcba0123c 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -37,7 +37,7 @@ module Google ## # This class manages APIs communication. class APIClient - include Google::Logging + include Google::APIClient::Logging ## # Creates a new Google API client. @@ -71,6 +71,8 @@ module Google # @option options [String] :discovery_path ("/discovery/v1") # The discovery base path. This rarely needs to be changed. def initialize(options={}) + logger.debug { "#{self.class} - Initializing client with options #{options}" } + # Normalize key to String to allow indifferent access. options = options.inject({}) do |accu, (key, value)| accu[key.to_sym] = value @@ -88,7 +90,7 @@ module Google app_version = options[:application_version] application_string = "#{app_name}/#{app_version || '0.0.0'}" else - logger.warn("Please provide :application_name and :application_version when initializing the APIClient") + logger.warn { "#{self.class} - Please provide :application_name and :application_version when initializing the client" } end self.user_agent = options[:user_agent] || ( "#{application_string} " + @@ -105,6 +107,7 @@ module Google @discovery_uris = {} @discovery_documents = {} @discovered_apis = {} + return self end @@ -560,6 +563,7 @@ module Google result = request.send(connection) if result.status == 401 && authorization.respond_to?(:refresh_token) && auto_refresh_token begin + logger.debug("Attempting refresh of access token & retry of request") authorization.fetch_access_token! result = request.send(connection) rescue Signet::AuthorizationError diff --git a/lib/google/api_client/logging.rb b/lib/google/api_client/logging.rb index fb3a7d902..09a075b5c 100644 --- a/lib/google/api_client/logging.rb +++ b/lib/google/api_client/logging.rb @@ -13,18 +13,20 @@ module Google self.logger = Logger.new(STDOUT) self.logger.level = Logger::WARN + + ## + # Module to make accessing the logger simpler + module Logging + ## + # Logger for the API client + # + # @return [Logger] logger instance. + def logger + Google::APIClient.logger + end + end + end - ## - # Module to make accessing the logger simpler - module Logging - ## - # Logger for the API client - # - # @return [Logger] logger instance. - def logger - Google::APIClient.logger - end - end end \ No newline at end of file diff --git a/lib/google/api_client/request.rb b/lib/google/api_client/request.rb index ce2f85438..9a084c31a 100644 --- a/lib/google/api_client/request.rb +++ b/lib/google/api_client/request.rb @@ -19,6 +19,7 @@ require 'compat/multi_json' require 'addressable/uri' require 'stringio' require 'google/api_client/discovery' +require 'google/api_client/logging' module Google class APIClient @@ -26,6 +27,8 @@ module Google ## # Represents an API request. class Request + include Google::APIClient::Logging + MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze # @return [Hash] Request parameters @@ -151,14 +154,19 @@ module Google # @return [Google::APIClient::Result] # result of API request def send(connection) - http_response = connection.app.call(self.to_env(connection)) + env = self.to_env(connection) + logger.debug { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" } + http_response = connection.app.call(env) result = self.process_http_response(http_response) + logger.debug { "#{self.class} Result: #{result.status} #{result.headers}" } + # Resumamble slightly different than other upload protocols in that it requires at least # 2 requests. if self.upload_type == 'resumable' upload = result.resumable_upload unless upload.complete? + logger.debug { "#{self.class} Sending upload body" } result = upload.send(connection) end end diff --git a/lib/google/api_client/version.rb b/lib/google/api_client/version.rb index dd3776acd..2e517f240 100644 --- a/lib/google/api_client/version.rb +++ b/lib/google/api_client/version.rb @@ -21,7 +21,7 @@ if !defined?(::Google::APIClient::VERSION) class APIClient module VERSION MAJOR = 0 - MINOR = 5 + MINOR = 6 TINY = 0 STRING = [MAJOR, MINOR, TINY].join('.') end