Class: Logger2
- Inherits:
-
Object
- Object
- Logger2
- 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
- .base(acolor1, acolor2, acolor3, name, message) ⇒ Object
-
.s_debug(message) ⇒ nil
Logs a debug message to the console.
-
.s_error(message) ⇒ nil
Logs an error to the console.
-
.s_fatal_error(message) ⇒ nil
Logs a fatal error to the console.
-
.s_info(message) ⇒ nil
Logs an info message to the console.
-
.s_warn(message) ⇒ nil
Logs a warning to the console.
Instance Method Summary collapse
-
#debug(message) ⇒ nil
Logs a debug message to the console if the verbosity level is set to 5.
-
#error(message) ⇒ nil
Logs an error to the console if the verbosity level is set to 2 or higher.
-
#fatal_error(message) ⇒ nil
Logs a fatal error to the console if the verbosity level is set to 1 or higher.
-
#info(message) ⇒ nil
Logs an info message to the console if the verbosity level is set to 4 or higher.
-
#initialize(verbosity_level) ⇒ Logger2
constructor
Creates a new Logger2 instance.
-
#warn(message) ⇒ nil
Logs a warning to the console if the verbosity level is set to 3 or higher.
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.
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, ) 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#{}\e[0m" end |
.s_debug(message) ⇒ nil
Logs a debug message to the console
99 100 101 |
# File 'lib/disrb/logger.rb', line 99 def self.s_debug() puts(base(155, 89, 182, 'DEBUG', )) end |
.s_error(message) ⇒ nil
Logs an error to the console
92 93 94 |
# File 'lib/disrb/logger.rb', line 92 def self.s_error() puts(base(243, 156, 18, 'ERROR', )) end |
.s_fatal_error(message) ⇒ nil
Logs a fatal error to the console
85 86 87 |
# File 'lib/disrb/logger.rb', line 85 def self.s_fatal_error() puts(base(192, 57, 43, 'FATAL ERROR', )) end |
.s_info(message) ⇒ nil
Logs an info message to the console
113 114 115 |
# File 'lib/disrb/logger.rb', line 113 def self.s_info() puts(base(76, 175, 80, 'INFORMATION', )) end |
.s_warn(message) ⇒ nil
Logs a warning to the console
106 107 108 |
# File 'lib/disrb/logger.rb', line 106 def self.s_warn() puts(base(241, 196, 15, 'WARNING', )) end |
Instance Method Details
#debug(message) ⇒ nil
Logs a debug message to the console if the verbosity level is set to 5.
58 59 60 61 62 |
# File 'lib/disrb/logger.rb', line 58 def debug() return unless @verbosity_level == 5 puts(Logger2.base(155, 89, 182, 'DEBUG', )) end |
#error(message) ⇒ nil
Logs an error to the console if the verbosity level is set to 2 or higher.
49 50 51 52 53 |
# File 'lib/disrb/logger.rb', line 49 def error() return unless @verbosity_level >= 2 puts(Logger2.base(243, 156, 18, 'ERROR', )) end |
#fatal_error(message) ⇒ nil
Logs a fatal error to the console if the verbosity level is set to 1 or higher.
40 41 42 43 44 |
# File 'lib/disrb/logger.rb', line 40 def fatal_error() return unless @verbosity_level >= 1 puts(Logger2.base(192, 57, 43, 'FATAL ERROR', )) end |