diff --git a/README.md b/README.md index ec8b69c79..1bdcf6aba 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,28 @@ GOOGLE_CLIENT_EMAIL="YOUR GOOGLE DEVELOPER EMAIL" GOOGLE_PRIVATE_KEY="YOUR GOOGLE DEVELOPER API KEY" ``` +## Logging + +The client includes a `Logger` instance that can be used to capture debugging information. + +To set the logging level for the client: + +```ruby +Google::Apis.logger.level = Logger::DEBUG +``` + +When running in a Rails environment, the client will default to using `::Rails.logger`. If you +prefer to use a separate logger instance for API calls, this can be changed via one of two ways. + +The first is to provide a new logger instance: + +```ruby +Google::Apis.logger = Logger.new(STDERR) +``` + +The second is to set the environment variable `GOOGLE_API_USE_RAILS_LOGGER` to any value other than `'true'` + + ## Samples Samples for versions 0.9 and onward can be found in the `samples` directory. diff --git a/lib/google/apis.rb b/lib/google/apis.rb index 72977af08..a37bd14d0 100644 --- a/lib/google/apis.rb +++ b/lib/google/apis.rb @@ -39,10 +39,20 @@ module Google logger end - # Check to see if client is being used in a Rails environment and ge the logger if present + # Check to see if client is being used in a Rails environment and get the logger if present. + # Setting the ENV variable 'GOOGLE_API_USE_RAILS_LOGGER' to false will force the client + # to use its own logger. + # # @return [Logger] def self.rails_logger - ::Rails.logger if defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger + if 'true' == ENV.fetch('GOOGLE_API_USE_RAILS_LOGGER', 'true') && + defined?(::Rails) && + ::Rails.respond_to?(:logger) && + !::Rails.logger.nil? + ::Rails.logger + else + nil + end end end end diff --git a/spec/google/apis/logging_spec.rb b/spec/google/apis/logging_spec.rb index 96d939a5a..3904b9078 100644 --- a/spec/google/apis/logging_spec.rb +++ b/spec/google/apis/logging_spec.rb @@ -16,6 +16,7 @@ require 'spec_helper' require 'google/apis' require 'google/apis/core/logging' + RSpec.describe Google::Apis do it 'should have a default logger' do expect(Google::Apis.logger).to be_an_instance_of(Logger) @@ -40,6 +41,60 @@ RSpec.describe Google::Apis do Google::Apis.logger = Logger.new(STDERR) expect(service.logger).to be Google::Apis.logger end + end + context 'with Rails' do + + before(:example) do + Google::Apis.logger = nil + Kernel.const_set('Rails', Module.new) unless defined?(::Rails) + end + + let(:logger) { Logger.new(STDERR) } + + let(:service) do + Class.new do + include Google::Apis::Core::Logging + end.new + end + + context 'with logger present' do + before(:example) do + allow(::Rails).to receive(:logger).and_return(logger) + end + + it 'should use the Rails logger' do + expect(service.logger).to be Rails.logger + end + end + + context 'with ENV bypass' do + before(:example) do + allow(::Rails).to receive(:logger).and_return(logger) + allow(::ENV).to receive(:fetch).and_return('false') + end + + it 'should use own logger' do + expect(service.logger).not_to be Rails.logger + end + + it 'should have a logger' do + expect(service.logger).to be_an_instance_of(Logger) + end + end + + context 'with logger not present' do + before(:example) do + allow(::Rails).to receive(:logger).and_return(nil) + end + + it 'should use own logger' do + expect(service.logger).not_to be Rails.logger + end + + it 'should have a logger' do + expect(service.logger).to be_an_instance_of(Logger) + end + end end end