Skip to content

Commit

Permalink
Allow users of this gem to override the logger
Browse files Browse the repository at this point in the history
  - This gem supports an existing OAUTH_DEBUG feature where log messages
  would be printed to $stdout when an ENV['OAUTH_DEBUG'] environment
  flag is raised.
  - Previously, the logging was hardcoded so as to write to $stdout.
  - With this change, the gem will offer users the ability to override
  the logger used. This should provide much more freedom, allowing users
  to more easily wrangle debug output by providing other loggers and
  using other log formatters.
  • Loading branch information
rthbound committed Jan 26, 2019
1 parent 58471c9 commit 8a13c6f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/oauth2/client.rb
Expand Up @@ -24,6 +24,7 @@ class Client # rubocop:disable Metrics/ClassLength
# @option opts [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
# @option opts [FixNum] :max_redirects (5) maximum number of redirects to follow
# @option opts [Boolean] :raise_errors (true) whether or not to raise an OAuth2::Error
# @options opts [Logger] :logger (::Logger.new($stdout)) which logger to use when OAUTH_DEBUG is enabled
# on responses with 400+ status codes
# @yield [builder] The Faraday connection builder
def initialize(client_id, client_secret, options = {}, &block)
Expand All @@ -39,7 +40,8 @@ def initialize(client_id, client_secret, options = {}, &block)
:connection_opts => {},
:connection_build => block,
:max_redirects => 5,
:raise_errors => true}.merge(opts)
:raise_errors => true,
:logger => ::Logger.new($stdout)}.merge!(opts)
@options[:connection_opts][:ssl] = ssl if ssl
end

Expand Down Expand Up @@ -223,7 +225,7 @@ def build_access_token(response, access_token_opts, access_token_class)
end

def oauth_debug_logging(builder)
builder.response :logger, ::Logger.new($stdout), :bodies => true if ENV['OAUTH_DEBUG'] == 'true'
builder.response :logger, options[:logger], :bodies => true if ENV['OAUTH_DEBUG'] == 'true'
end
end
end
30 changes: 30 additions & 0 deletions spec/oauth2/client_spec.rb
Expand Up @@ -227,6 +227,36 @@
expect(response.headers).to eq('Content-Type' => 'text/awesome')
end

context 'when OAUTH_DEBUG=true and logger is set to log to /dev/null' do
around do |example|
begin
original = ENV['OAUTH_DEBUG']
ENV['OAUTH_DEBUG'] = 'true'

original_logger = subject.options[:logger]
subject.options[:logger] = Logger.new('/dev/null')

example.call
ensure
subject.options[:logger] = original_logger

if original.nil?
ENV.delete('OAUTH_DEBUG')
else
ENV['OAUTH_DEBUG'] = original
end
end
end

it 'will not log anything to standard out if logger is overridden to use /dev/null' do
output = capture_output do
subject.request(:get, '/success')
end

expect(output).to be_empty
end
end

context 'when OAUTH_DEBUG=true' do
around do |example|
begin
Expand Down

0 comments on commit 8a13c6f

Please sign in to comment.