Class: Dragnet::CLI::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/dragnet/cli/logger.rb

Overview

A logger for the CLI. It uses the say method in Thor's Shell class to print the messages to the output, honoring the status of the quiet command line switch.

Constant Summary collapse

LEVELS =
{ debug: 0, info: 1, warn: 2, error: 3 }.freeze
DEFAULT_LOG_LEVEL =
:info
PADDING_STRING =
' '
PADDING_WIDTH =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell, log_level = DEFAULT_LOG_LEVEL) ⇒ Logger

Creates a new instance of the class.

Parameters:

  • shell (Thor::Shell::Basic)

    A reference to Thor's Shell this will be used to send the output to the terminal in which Thor was started.

  • log_level (Symbol) (defaults to: DEFAULT_LOG_LEVEL)

    The log level for the logger. The higher the level the less output will be printed.

Raises:

  • (ArgumentError)

See Also:



25
26
27
28
29
30
# File 'lib/dragnet/cli/logger.rb', line 25

def initialize(shell, log_level = DEFAULT_LOG_LEVEL)
  raise ArgumentError, "Unknown logger level: #{log_level}" unless LEVELS.keys.include?(log_level)

  @log_level = LEVELS[log_level]
  @shell = shell
end

Instance Attribute Details

#log_levelObject (readonly)

Returns the value of attribute log_level.



11
12
13
# File 'lib/dragnet/cli/logger.rb', line 11

def log_level
  @log_level
end

#shellObject (readonly)

Returns the value of attribute shell.



11
12
13
# File 'lib/dragnet/cli/logger.rb', line 11

def shell
  @shell
end

Instance Method Details

#debug(message) ⇒ Object

Prints a message with log level debug

Parameters:

  • message (String)

    The message to print



34
35
36
# File 'lib/dragnet/cli/logger.rb', line 34

def debug(message)
  output(:debug, :green, message)
end

#error(message) ⇒ Object

Prints a message with log level error

Parameters:

  • message (String)

    The message to print



52
53
54
# File 'lib/dragnet/cli/logger.rb', line 52

def error(message)
  output(:error, :red, message)
end

#info(message) ⇒ Object

Prints a message with log level info

Parameters:

  • message (String)

    The message to print



40
41
42
# File 'lib/dragnet/cli/logger.rb', line 40

def info(message)
  output(:info, :blue, message)
end

#warn(message) ⇒ Object

Prints a message with log level warn

Parameters:

  • message (String)

    The message to print



46
47
48
# File 'lib/dragnet/cli/logger.rb', line 46

def warn(message)
  output(:warn, :yellow, message)
end