Class: Mercury::CLI::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mercury/cli/logger.rb

Overview

Mercury::CLI::Logger is a singleton class responsible for generating feedback in the terminal.

Constant Summary collapse

ACTION_COLORS =

ANSI console codes (escape sequences) for highlighting particular log outputs.

{
  :error          => "\e[1m" + "\e[31m", # bold + red
  :warning        => "\e[1m" + "\e[33m", # bold + yellow
  :info           => "\e[1m" + "\e[32m", # bold + green
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



44
45
46
47
# File 'lib/mercury/cli/logger.rb', line 44

def initialize
  @level = :high
  @color = true
end

Instance Attribute Details

#colorObject Also known as: color?

Whether to use color in log messages or not



41
42
43
# File 'lib/mercury/cli/logger.rb', line 41

def color
  @color
end

#levelObject

The log level, which can be :high, :low or :off (which will log all messages, only high-priority messages, or no messages at all, respectively).



38
39
40
# File 'lib/mercury/cli/logger.rb', line 38

def level
  @level
end

Instance Method Details

#log(level, message, io = $stdout) ⇒ Object

Logs a message.

level

The importance of this message. Can be :high or :low.

message

The message to be logged.

io

The IO instance to which the message will be written. Defaults to standard output.



78
79
80
81
82
83
84
# File 'lib/mercury/cli/logger.rb', line 78

def log(level, message, io=$stdout)
  # Don't log when logging is disabled
  return if @level == :off

  # Log when level permits it
  io.puts(message) if (@level == :low or @level == level)
end

#log_level(level, action, message) ⇒ Object

Logs a messsage, using appropriate colours to highlight different levels.

level

The importance of this action. Can be :high or :low.

action

The kind of file action. Can be :create, :update or :identical.

message

The identifier of the item the action was performed on.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mercury/cli/logger.rb', line 58

def log_level(level, action, message)
  log(
    level,
    '%s%12s%s:  %s' % [
      color? ? ACTION_COLORS[action.to_sym] : '',
      action.capitalize,
      color? ? "\e[0m" : '',
      message.word_wrap(60).indent(15).lstrip
    ]
  )
end