Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Commit 12103c2

Browse files
authored
SRCH-1837 add Elasticsearch client logging to Rails log (#73)
1 parent e502125 commit 12103c2

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

config/elasticsearch.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ development: &default
44
user: ''
55
password: ''
66
number_of_shards: 1
7+
log: true
8+
log_level: DEBUG
79

810
test:
911
<<: *default
12+
13+
production:
14+
# Changes to the production configuration must be
15+
# made in the cookbooks

config/initializers/elasticsearch.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
Elasticsearch::Persistence.client = Elasticsearch::Client.new(
4-
log: Rails.env.development?,
4+
log: Rails.configuration.elasticsearch['log'],
55
hosts: Rails.configuration.elasticsearch['hosts'],
66
user: Rails.configuration.elasticsearch['user'],
77
password: Rails.configuration.elasticsearch['password'],
@@ -10,12 +10,14 @@
1010
reload_connections: true
1111
)
1212

13-
if Rails.env.development?
14-
logger = ActiveSupport::Logger.new(STDERR)
15-
logger.level = Logger::DEBUG
16-
logger.formatter = proc { |_s, _d, _p, m| "\e[2m#{m}\n\e[0m" }
17-
Elasticsearch::Persistence.client.transport.logger = logger
13+
logger = ActiveSupport::Logger.new("log/#{Rails.env}.log")
14+
logger.level = Rails.configuration.elasticsearch['log_level']
15+
logger.formatter = proc do |severity, time, _progname, msg|
16+
"\e[2m[ES][#{time.utc.iso8601(6)}][#{severity}] #{msg}\n\e[0m"
17+
end
18+
Elasticsearch::Persistence.client.transport.logger = logger
1819

20+
if Rails.env.development?
1921
puts 'Ensuring Elasticsearch development indexes and aliases are available....'
2022
Dir[Rails.root.join('app', 'models', '*.rb')].map do |f|
2123
klass = File.basename(f, '.*').camelize.constantize

0 commit comments

Comments
 (0)