Class: HTMLProofer::Log

Inherits:
Object
  • Object
show all
Includes:
Yell::Loggable
Defined in:
lib/html_proofer/log.rb

Constant Summary collapse

STDOUT_LEVELS =
[:debug, :info, :warn].freeze
STDERR_LEVELS =
[:error, :fatal].freeze

Instance Method Summary collapse

Constructor Details

#initialize(log_level) ⇒ Log

Returns a new instance of Log.



13
14
15
16
17
18
19
20
21
22
# File 'lib/html_proofer/log.rb', line 13

def initialize(log_level)
  @logger = Yell.new(
    format: false,
    name: "HTMLProofer", \
    level: "gte.#{log_level}",
  ) do |l|
    l.adapter(:stdout, level: "lte.warn")
    l.adapter(:stderr, level: "gte.error")
  end
end

Instance Method Details

#colorize(level, message) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/html_proofer/log.rb', line 32

def colorize(level, message)
  color = case level
  when :debug
    :cyan
  when :info
    :blue
  when :warn
    :yellow
  when :error, :fatal
    :red
  end

  if STDOUT_LEVELS.include?(level) ||
      STDERR_LEVELS.include?(level)
    Rainbow(message).send(color)
  else
    message
  end
end

#debug(message = nil) ⇒ Object

dumb override to play nice with Typhoeus/Ethon



53
54
55
# File 'lib/html_proofer/log.rb', line 53

def debug(message = nil)
  log(:debug, message) unless message.nil?
end

#log(level, message) ⇒ Object



24
25
26
# File 'lib/html_proofer/log.rb', line 24

def log(level, message)
  log_with_color(level, message)
end

#log_with_color(level, message) ⇒ Object



28
29
30
# File 'lib/html_proofer/log.rb', line 28

def log_with_color(level, message)
  @logger.send(level, colorize(level, message))
end