Skip to content

Commit

Permalink
Standardize on logging and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Dec 24, 2015
1 parent e245d4e commit 46c9eb8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lib/html-proofer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lib/html-proofer/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/html-proofer/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions lib/html-proofer/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
8 changes: 4 additions & 4 deletions lib/html-proofer/url_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 46c9eb8

Please sign in to comment.