Class: HTMLProofer::Log

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

Constant Summary collapse

STDOUT_LEVELS =
%i[debug info warn].freeze
STDERR_LEVELS =
%i[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
# 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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/html-proofer/log.rb', line 30

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) && $stdout.isatty) || \
     (STDERR_LEVELS.include?(level) && $stderr.isatty)
    Rainbow(message).send(color)
  else
    message
  end
end

#debug(message = nil) ⇒ Object

dumb override to play nice with Typhoeus/Ethon



51
52
53
# File 'lib/html-proofer/log.rb', line 51

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

#log(level, message) ⇒ Object



22
23
24
# File 'lib/html-proofer/log.rb', line 22

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

#log_with_color(level, message) ⇒ Object



26
27
28
# File 'lib/html-proofer/log.rb', line 26

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