Class: Logger2

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

Overview

Logging handler with some cool styling (called Logger2 because Logger is a built-in Ruby class)

When you create an instance of this class, you need to set the verbosity level, and when you run the instance methods, it will follow the set verbosity level. Example: you create a Logger2 instance with verbosity level 3 (warning), only the methods “fatal_error”, “error” and “warn” will print to the console. The class methods (start with s_) will always print to the console.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbosity_level) ⇒ Logger2

Creates a new Logger2 instance.

verbosity_level can be set to:

  • 0: No logging.

  • 1: Fatal errors only.

  • 2: Fatal errors and errors.

  • 3: All of the above and warnings.

  • 4: All of the above and information messages.

  • 5: All of the above and debug messages.

Parameters:

  • verbosity_level (Integer)

    The verbosity level for the logger to follow.



21
22
23
# File 'lib/disrb/logger.rb', line 21

def initialize(verbosity_level)
  @verbosity_level = verbosity_level
end

Class Method Details

.base(acolor1, acolor2, acolor3, name, message) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/disrb/logger.rb', line 25

def self.base(acolor1, acolor2, acolor3, name, message)
  caller = caller_locations[1]
  file = caller.path
  line = caller.lineno
  location = "#{file}:#{line}"
  name = name.ljust(14, ' ')
  acolors = [acolor1, acolor2, acolor3].join(';')
  "\033[1;38;2;255;255;255;48;2;#{acolors}m | #{name} \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
    " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} at #{location} \033[0m\033[1;38;2;255;255;255;48;2;#{acolors}m" \
    "  \033[0m \e[38;2;#{acolors}m#{message}\e[0m"
end

.s_debug(message) ⇒ nil

Logs a debug message to the console

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


99
100
101
# File 'lib/disrb/logger.rb', line 99

def self.s_debug(message)
  puts(base(155, 89, 182, 'DEBUG', message))
end

.s_error(message) ⇒ nil

Logs an error to the console

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


92
93
94
# File 'lib/disrb/logger.rb', line 92

def self.s_error(message)
  puts(base(243, 156, 18, 'ERROR', message))
end

.s_fatal_error(message) ⇒ nil

Logs a fatal error to the console

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


85
86
87
# File 'lib/disrb/logger.rb', line 85

def self.s_fatal_error(message)
  puts(base(192, 57, 43, 'FATAL ERROR', message))
end

.s_info(message) ⇒ nil

Logs an info message to the console

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


113
114
115
# File 'lib/disrb/logger.rb', line 113

def self.s_info(message)
  puts(base(76, 175, 80, 'INFORMATION', message))
end

.s_warn(message) ⇒ nil

Logs a warning to the console

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


106
107
108
# File 'lib/disrb/logger.rb', line 106

def self.s_warn(message)
  puts(base(241, 196, 15, 'WARNING', message))
end

Instance Method Details

#debug(message) ⇒ nil

Logs a debug message to the console if the verbosity level is set to 5.

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


58
59
60
61
62
# File 'lib/disrb/logger.rb', line 58

def debug(message)
  return unless @verbosity_level == 5

  puts(Logger2.base(155, 89, 182, 'DEBUG', message))
end

#error(message) ⇒ nil

Logs an error to the console if the verbosity level is set to 2 or higher.

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


49
50
51
52
53
# File 'lib/disrb/logger.rb', line 49

def error(message)
  return unless @verbosity_level >= 2

  puts(Logger2.base(243, 156, 18, 'ERROR', message))
end

#fatal_error(message) ⇒ nil

Logs a fatal error to the console if the verbosity level is set to 1 or higher.

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


40
41
42
43
44
# File 'lib/disrb/logger.rb', line 40

def fatal_error(message)
  return unless @verbosity_level >= 1

  puts(Logger2.base(192, 57, 43, 'FATAL ERROR', message))
end

#info(message) ⇒ nil

Logs an info message to the console if the verbosity level is set to 4 or higher.

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


76
77
78
79
80
# File 'lib/disrb/logger.rb', line 76

def info(message)
  return unless @verbosity_level >= 4

  puts(Logger2.base(76, 175, 80, 'INFORMATION', message))
end

#warn(message) ⇒ nil

Logs a warning to the console if the verbosity level is set to 3 or higher.

Parameters:

  • message (String)

    The message to log.

Returns:

  • (nil)


67
68
69
70
71
# File 'lib/disrb/logger.rb', line 67

def warn(message)
  return unless @verbosity_level >= 3

  puts(Logger2.base(241, 196, 15, 'WARNING', message))
end