Module: CliLogger::Logger

Included in:
CliLogger
Defined in:
lib/cli_logger/logger.rb

Overview

Logger module

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

rubocop:enable Style/AccessorMethodName



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

def method_missing(name, *args, &block)
  log(name.to_sym, *args, &block) if levels.key?(name.to_sym)
end

Instance Method Details

#current_levelObject



30
31
32
# File 'lib/cli_logger/logger.rb', line 30

def current_level
  @current_level ||= levels[:info]
end

#current_level=(value) ⇒ Object



34
35
36
37
# File 'lib/cli_logger/logger.rb', line 34

def current_level=(value)
  validate_level(value)
  @current_level = levels[value]
end

#levelsObject



39
40
41
42
43
44
45
46
# File 'lib/cli_logger/logger.rb', line 39

def levels
  {
    error: 0,
    warning: 1,
    info: 2,
    debug: 3
  }
end

#log(log_level, message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cli_logger/logger.rb', line 15

def log(log_level, message)
  validate_level(log_level)
  return unless levels[log_level] <= current_level

  message = log_message(log_level, message)
  if block_given?
    sublog do
      yield progress_bar
    end
    message += log_message(log_level, 'DONE')
  end

  message
end

#set_options(*args) ⇒ Object

rubocop:disable Style/AccessorMethodName



6
7
8
# File 'lib/cli_logger/logger.rb', line 6

def set_options(*args)
  @options = args
end