#389 - Add option to bypass Rails logger + update readme
This commit is contained in:
		
							parent
							
								
									e3d2a16a19
								
							
						
					
					
						commit
						4e19ae601f
					
				
							
								
								
									
										22
									
								
								README.md
								
								
								
								
							
							
						
						
									
										22
									
								
								README.md
								
								
								
								
							|  | @ -252,6 +252,28 @@ GOOGLE_CLIENT_EMAIL="YOUR GOOGLE DEVELOPER EMAIL" | ||||||
| GOOGLE_PRIVATE_KEY="YOUR GOOGLE DEVELOPER API KEY" | 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 | ||||||
| 
 | 
 | ||||||
| Samples for versions 0.9 and onward can be found in the `samples` directory. | Samples for versions 0.9 and onward can be found in the `samples` directory. | ||||||
|  |  | ||||||
|  | @ -39,10 +39,20 @@ module Google | ||||||
|       logger |       logger | ||||||
|     end |     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] |     # @return [Logger] | ||||||
|     def self.rails_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 |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ require 'spec_helper' | ||||||
| require 'google/apis' | require 'google/apis' | ||||||
| require 'google/apis/core/logging' | require 'google/apis/core/logging' | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| RSpec.describe Google::Apis do | RSpec.describe Google::Apis do | ||||||
|   it 'should have a default logger' do |   it 'should have a default logger' do | ||||||
|     expect(Google::Apis.logger).to be_an_instance_of(Logger) |     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) |       Google::Apis.logger = Logger.new(STDERR) | ||||||
|       expect(service.logger).to be Google::Apis.logger |       expect(service.logger).to be Google::Apis.logger | ||||||
|     end |     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 | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue