From 46c9eb84928e783a0590afc9bb94a656132e28b4 Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Thu, 24 Dec 2015 12:56:38 -0600 Subject: [PATCH] Standardize on logging and colors --- lib/html-proofer.rb | 8 ++++---- lib/html-proofer/cache.rb | 8 ++++---- lib/html-proofer/issue.rb | 6 +++--- lib/html-proofer/log.rb | 19 +++++++++++++++++-- lib/html-proofer/url_validator.rb | 8 ++++---- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/html-proofer.rb b/lib/html-proofer.rb index 22e1f7ff..dd886d1c 100644 --- a/lib/html-proofer.rb +++ b/lib/html-proofer.rb @@ -40,18 +40,18 @@ def initialize(src, opts = {}) end def run - @logger.log :info, :blue, "Running #{checks} on #{@src} on *#{@options[:ext]}... \n\n" + @logger.log :info, "Running #{checks} on #{@src} on *#{@options[:ext]}... \n\n" if @src.is_a?(Array) && !@options[:disable_external] check_list_of_links else check_files_in_directory file_text = pluralize(files.length, 'file', 'files') - @logger.log :info, :blue, "Ran on #{file_text}!\n\n" + @logger.log :info, "Ran on #{file_text}!\n\n" end if @failed_tests.empty? - @logger.log :info, :green, 'HTML-Proofer finished successfully.' + @logger.log_with_color :info, :green, 'HTML-Proofer finished successfully.' else print_failed_tests end @@ -96,7 +96,7 @@ def process_files html = create_nokogiri(path) checks.each do |klass| - @logger.log :debug, :yellow, "Checking #{klass.to_s.downcase} on #{path} ..." + @logger.log :debug, "Checking #{klass.to_s.downcase} on #{path} ..." check = Object.const_get(klass).new(@src, path, html, @options) check.run result[:external_urls].merge!(check.external_urls) diff --git a/lib/html-proofer/cache.rb b/lib/html-proofer/cache.rb index b37df9b5..58619731 100644 --- a/lib/html-proofer/cache.rb +++ b/lib/html-proofer/cache.rb @@ -84,21 +84,21 @@ def detect_url_changes(found) if existing_urls.include?(url) true else - @logger.log :debug, :yellow, "Adding #{url} to cache check" + @logger.log :debug, "Adding #{url} to cache check" false end end new_link_count = additions.length new_link_text = pluralize(new_link_count, 'link', 'links') - @logger.log :info, :blue, "Adding #{new_link_text} to the cache..." + @logger.log :info, "Adding #{new_link_text} to the cache..." # remove from cache URLs that no longer exist del = 0 @cache_log.delete_if do |url, _| url = clean_url(url) if !found_urls.include?(url) - @logger.log :debug, :yellow, "Removing #{url} from cache check" + @logger.log :debug, "Removing #{url} from cache check" del += 1 true else @@ -107,7 +107,7 @@ def detect_url_changes(found) end del_link_text = pluralize(del, 'link', 'links') - @logger.log :info, :blue, "Removing #{del_link_text} from the cache..." + @logger.log :info, "Removing #{del_link_text} from the cache..." additions end diff --git a/lib/html-proofer/issue.rb b/lib/html-proofer/issue.rb index 6fe060da..ce34e96d 100644 --- a/lib/html-proofer/issue.rb +++ b/lib/html-proofer/issue.rb @@ -46,13 +46,13 @@ def report(sorted_issues, first_report, second_report) sorted_issues.each do |issue| if matcher != issue.send(first_report) - @logger.log :error, :red, "- #{issue.send(first_report)}" + @logger.log :error, "- #{issue.send(first_report)}" matcher = issue.send(first_report) end if first_report == :status - @logger.log :error, :red, " * #{issue}" + @logger.log :error, " * #{issue}" else - @logger.log :error, :red, " * #{issue.send(second_report)}#{issue.line_number}" + @logger.log :error, " * #{issue.send(second_report)}#{issue.line_number}" end end end diff --git a/lib/html-proofer/log.rb b/lib/html-proofer/log.rb index 63182b23..7824f19a 100644 --- a/lib/html-proofer/log.rb +++ b/lib/html-proofer/log.rb @@ -14,7 +14,22 @@ def initialize(log_level) end end - def log(level, color, message) + def log(level, message) + color = case level + when :debug + :light_blue + when :info + :blue + when :warn + :yellow + when :error, :fatal + :red + end + + log_with_color(level, color, message) + end + + def log_with_color(level, color, message) @logger.send level, colorize(color, message) end @@ -28,7 +43,7 @@ def colorize(color, message) # dumb override to play nice with Typhoeus/Ethon def debug(message = nil) - log(:debug, :yellow, message) unless message.nil? + log(:debug, message) unless message.nil? end end end diff --git a/lib/html-proofer/url_validator.rb b/lib/html-proofer/url_validator.rb index eb70e175..21f06e64 100644 --- a/lib/html-proofer/url_validator.rb +++ b/lib/html-proofer/url_validator.rb @@ -40,7 +40,7 @@ def remove_query_values uri = begin Addressable::URI.parse(url) rescue URI::Error, Addressable::URI::InvalidURIError - @logger.log :error, :red, "#{url} is an invalid URL" + @logger.log :error, "#{url} is an invalid URL" nil end next if uri.nil? || uri.query.nil? @@ -72,7 +72,7 @@ def load_cache cache_count = @cache.size cache_text = pluralize(cache_count, 'link', 'links') - @logger.log :info, :blue, "Found #{cache_text} in the cache..." + @logger.log :info, "Found #{cache_text} in the cache..." @cache.retrieve_urls(@external_urls) end @@ -92,7 +92,7 @@ def external_link_checker(external_urls) count = external_urls.length check_text = pluralize(count, 'external link', 'external links') - @logger.log :info, :blue, "Checking #{check_text}..." + @logger.log :info, "Checking #{check_text}..." # Route log from Typhoeus/Ethon to our own logger Ethon.logger = @logger @@ -139,7 +139,7 @@ def response_handler(response, filenames) debug_msg = "Received a #{response_code} for #{href}" debug_msg << " in #{filenames.join(' ')}" unless filenames.nil? - @logger.log :debug, :yellow, debug_msg + @logger.log :debug, debug_msg if @options[:http_status_ignore].include?(response_code) # no op