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 16, 2021
1 parent b77e6fa commit c13f432
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ gem 'doorkeeper' # Awesome OAuth 2 provider for your Rails / Grape app
gem 'webpacker', '~>5.2.0' # Transpile app-like JavaScript
gem 'sass-rails' # SASS

# Logging tools
gem 'colorize' # Ruby gem for colorizing text using ANSI escape sequences

# Translations
# gem 'devise-i18n' # Translations for Devise
# gem 'rails-i18n', '~> 6.0.0' # Translations for Rails
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ GEM
sexp_processor
coderay (1.1.3)
colored2 (3.1.2)
colorize (0.8.1)
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
cork (0.3.0)
Expand Down Expand Up @@ -478,6 +479,7 @@ DEPENDENCIES
brakeman
bullet
capybara (>= 2.15)
colorize
danger
danger-brakeman_scanner
danger-eslint
Expand Down
4 changes: 3 additions & 1 deletion app/services/google_service/client_service.rb
Original file line number Diff line number Diff line change
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 @@ -16,7 +17,7 @@ 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
Expand All @@ -33,6 +34,7 @@ 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
end
Expand Down

0 comments on commit c13f432

Please sign in to comment.