Class: XCRes::Logger
- Inherits:
-
Object
- Object
- XCRes::Logger
- Defined in:
- lib/xcres/logger.rb
Overview
A Logger utility help class
Instance Attribute Summary collapse
-
#colored ⇒ Bool
(also: #colored?)
If set to true, ANSI colors will be used to color the output otherwise it will output plain text, true by default.
-
#indentation ⇒ String
The indentation of the output, empty string by default.
-
#silent ⇒ Bool
(also: #silent?)
If set to true, all log calls of all kinds will be ignored otherwise they will be printed, false by default.
-
#verbose ⇒ Bool
(also: #verbose?)
If set to false, calls to #log will be ignored otherwise they will be printed, false by default.
Instance Method Summary collapse
-
#fail(message_or_exception, *format_args) ⇒ Object
Print a log message to indicate failure of an operation in red color.
-
#inform(message, *format_args) ⇒ Object
Prints a formatted message.
-
#inform_colored(message, color, *format_args) ⇒ Object
Prints a formatted message in a given color, and prints its arguments with bold font weight.
-
#initialize ⇒ Logger
constructor
Initialize a new logger.
-
#log(message, *format_args) ⇒ Object
Print a log message of log level verbose.
-
#success(message, *format_args) ⇒ Object
Print a log message to indicate success of an operation in green color.
-
#warn(message_or_exception, *format_args) ⇒ Object
Print a warning log message in yellow color.
Constructor Details
#initialize ⇒ Logger
Initialize a new logger
33 34 35 36 37 38 |
# File 'lib/xcres/logger.rb', line 33 def initialize self.silent = false self.verbose = false self.colored = true self.indentation = '' end |
Instance Attribute Details
#colored ⇒ Bool Also known as: colored?
Returns if set to true, ANSI colors will be used to color the output otherwise it will output plain text, true by default.
24 25 26 |
# File 'lib/xcres/logger.rb', line 24 def colored @colored end |
#indentation ⇒ String
Returns the indentation of the output, empty string by default.
29 30 31 |
# File 'lib/xcres/logger.rb', line 29 def indentation @indentation end |
#silent ⇒ Bool Also known as: silent?
Returns if set to true, all log calls of all kinds will be ignored otherwise they will be printed, false by default.
18 19 20 |
# File 'lib/xcres/logger.rb', line 18 def silent @silent end |
#verbose ⇒ Bool Also known as: verbose?
Returns if set to false, calls to #log will be ignored otherwise they will be printed, false by default.
12 13 14 |
# File 'lib/xcres/logger.rb', line 12 def verbose @verbose end |
Instance Method Details
#fail(message_or_exception, *format_args) ⇒ Object
Print a log message to indicate failure of an operation in red color
135 136 137 138 139 140 141 142 |
# File 'lib/xcres/logger.rb', line 135 def fail , *format_args , exception = () inform_colored '✗' + ' ' + , :red, *format_args if verbose? && exception != nil log "Backtrace:\n"+exception.backtrace.join("\n"), :red end end |
#inform(message, *format_args) ⇒ Object
Prints a formatted message
49 50 51 |
# File 'lib/xcres/logger.rb', line 49 def inform , *format_args puts indentation + % format_args unless silent? end |
#inform_colored(message, color, *format_args) ⇒ Object
Prints a formatted message in a given color, and prints its arguments with bold font weight
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/xcres/logger.rb', line 66 def inform_colored , color, *format_args if colored? parts = .gsub(/%[\ +#]?\d*\.?\d*[a-z]/, "\0"+'\0'+"\0") .split("\0") .reject(&:empty?) = parts.map do |part| if part[0] == '%' && part[1] != '%' (part % [format_args.shift]).bold.gsub('%', '%%') else part end end.map(&color).join('') end inform , *format_args end |
#log(message, *format_args) ⇒ Object
Print a log message of log level verbose
92 93 94 |
# File 'lib/xcres/logger.rb', line 92 def log , *format_args inform_colored 'Ⓥ' + ' ' + , :magenta, *format_args if verbose? end |
#success(message, *format_args) ⇒ Object
Print a log message to indicate success of an operation in green color
105 106 107 |
# File 'lib/xcres/logger.rb', line 105 def success , *format_args inform_colored '✓' + ' ' + , :green, *format_args end |
#warn(message_or_exception, *format_args) ⇒ Object
Print a warning log message in yellow color
118 119 120 121 |
# File 'lib/xcres/logger.rb', line 118 def warn , *format_args , _ = () inform_colored '⚠' + ' ' + , :yellow, *format_args end |