Skip to content

Commit

Permalink
Add configuration for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cdmwebs committed Jul 12, 2023
1 parent ff103b5 commit 7948b7b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fulfil/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def request(endpoint:, verb: :get, **args)
end

def client
client = HTTP.use(logging: @debug ? { logger: Logger.new($stdout) } : {})
client = HTTP.use(logging: @debug ? { logger: config.logger } : {})
client = client.auth("Bearer #{@token}") if @token
client.headers(@headers)
end
Expand Down
14 changes: 14 additions & 0 deletions lib/fulfil/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'logger'

module Fulfil
# The `Fulfil::Configuration` contains the available configuration options
# for the `Fulfil` gem.
Expand All @@ -22,9 +24,21 @@ class Configuration
# @return [Proc, nil]
attr_accessor :rate_limit_notification_handler

# Allows the client to configure a logger. Logs output to STDOUT by default.
#
# @return [Logger, nil]
#
# @example Use a logger to log the API rate limit hits
# Fulfil.configure do |config|
# config.logger = Logger.new(STDOUT)
# end
#
attr_accessor :logger

def initialize
@retry_on_rate_limit = false
@retry_on_rate_limit_wait = 1
@logger = Logger.new($stdout)
end

def retry_on_rate_limit?
Expand Down
12 changes: 12 additions & 0 deletions test/fulfil/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@ def test_retry_on_rate_limit?
assert_predicate Fulfil.config, :retry_on_rate_limit?
end
end

def test_default_logger
assert_instance_of Logger, Fulfil.config.logger
end

def test_configured_logger
with_fulfil_config do |config|
config.logger = Logger.new('test.log')
end

assert_instance_of Logger, Fulfil.config.logger
end
end
end

0 comments on commit 7948b7b

Please sign in to comment.