Created
July 3, 2015 15:58
-
-
Save andxyz/bbd58d7031ae0522a2c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SearchApp | |
class ElasticSearchClient | |
def self.production_client | |
Elasticsearch::Client.new | |
end | |
def self.debug_client | |
@client = Elasticsearch::Client.new log: true | |
@client.transport.logger = Logger.new($stdout) | |
@client.transport.logger.progname = 'ElasticSearchClient' | |
@client.transport.logger.formatter = proc do |serverity, time, progname, msg| | |
puts ["[#{time.utc.iso8601}]", serverity, progname, format_msg(msg)].join(' ') | |
end | |
@client | |
end | |
private | |
# requests start with "> " | |
# responses start with "< " | |
def self.format_msg(msg) | |
if msg.start_with?('< ') | |
'< ' + JSON.pretty_generate(JSON.parse(msg[2..-1])) | |
elsif msg.start_with?('> ') | |
'> ' + JSON.pretty_generate(JSON.parse(msg[2..-1])) | |
else | |
msg | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment