Skip to content

Commit

Permalink
[#6] Colorize logs from Rails.logger
Browse files Browse the repository at this point in the history
  • Loading branch information
malparty committed Jun 28, 2021
1 parent c8390a6 commit 22617cd
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/services/google_service/client_service.rb
Expand Up @@ -3,6 +3,7 @@
module GoogleService
class ClientService
require 'httparty'
require 'colorize'

USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '\
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
Expand All @@ -26,12 +27,28 @@ def query_result
begin
@result = HTTParty.get(@uri, { headers: { 'User-Agent' => USER_AGENT } })
rescue HTTParty::Error, Timeout::Error, SocketError => e
Rails.logger.error "Error: Query Google with keyword #{@keyword} throw an error: #{e}"
Rails.logger.error "Error: Query Google with keyword #{@keyword} throw an error: #{e}".colorize(:red)
@result = nil
else
validate_result
end
@result
end
<<<<<<< HEAD
=======

private

# Inspect Http response status code
# Any non 200 response code will be logged
# response is set to nil in order to notify the error
def validate_result
return if @result.response.code == '200'

Rails.logger.warn "Warning: Query Google with keyword #{@keyword} return status code #{@result.response.code}"
.colorize(:yellow)
@result = nil
end
>>>>>>> d94c7d6 ([#6] Colorize logs from Rails.logger)
end
end

0 comments on commit 22617cd

Please sign in to comment.