Skip to content

Commit 1a2c572

Browse files
authored
fix(logging): pass user configured logger to temp client (#480)
The `replace_all_objects` command creates a temporary client which does not have the some configuration as one constructed by users of the library. This change adds an optional parameter when constructing an index for the `logger` and allows that to be passed in to the temporary client. This change also ensures that any `Index` built through the `SearchClient#init_index` call gets the logger configured on the client. This will ensure logging is done in a consistent manner across clients instantiated by the library.
1 parent fadea61 commit 1a2c572

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lib/algolia/search_client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Client
1919
def initialize(search_config, opts = {})
2020
@config = search_config
2121
adapter = opts[:adapter] || Defaults::ADAPTER
22-
logger = opts[:logger] || LoggerHelper.create
23-
requester = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter, logger)
22+
@logger = opts[:logger] || LoggerHelper.create
23+
requester = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter, @logger)
2424
@transporter = Transport::Transport.new(@config, requester)
2525
end
2626

@@ -94,7 +94,7 @@ def init_index(index_name)
9494
if stripped_index_name.empty?
9595
raise AlgoliaError, 'Please provide a valid index name'
9696
end
97-
Index.new(stripped_index_name, @transporter, @config)
97+
Index.new(stripped_index_name, @transporter, @config, @logger)
9898
end
9999

100100
# List all indexes of the client

lib/algolia/search_index.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ class Index
55
include CallType
66
include Helpers
77

8-
attr_reader :name, :transporter, :config
8+
attr_reader :name, :transporter, :config, :logger
99

1010
# Initialize an index
1111
#
1212
# @param name [String] name of the index
1313
# @param transporter [Object] transport object used for the connection
1414
# @param config [Config] a Config object which contains your APP_ID and API_KEY
15+
# @param logger [LoggerHelper] an optional LoggerHelper object to use
1516
#
16-
def initialize(name, transporter, config)
17+
def initialize(name, transporter, config, logger = nil)
1718
@name = name
1819
@transporter = transporter
1920
@config = config
21+
@logger = logger || LoggerHelper.create
2022
end
2123

2224
# # # # # # # # # # # # # # # # # # # # #
@@ -830,7 +832,7 @@ def replace_all_objects(objects, opts = {})
830832
end
831833

832834
# TODO: consider create a new client with state of retry is shared
833-
tmp_client = Algolia::Search::Client.new(@config)
835+
tmp_client = Algolia::Search::Client.new(@config, { logger: logger })
834836
tmp_index = tmp_client.init_index(tmp_index_name)
835837

836838
save_objects_response = tmp_index.save_objects(objects, request_options)

0 commit comments

Comments
 (0)