Module: SecureKeys::Core::Console::Logger
- Defined in:
- lib/core/console/logger.rb
Class Method Summary collapse
-
.command(command:) ⇒ Object
Log a command message.
-
.command_output(command:) ⇒ Object
Log a command output.
-
.crash!(message:) ⇒ Object
Crash the terminal with a message.
-
.deprecated(message:) ⇒ Object
Log a deprecated message.
-
.encode_as_utf_8_if_possible(message:) ⇒ String
Encode a message as UTF-8 if possible.
-
.error(message:) ⇒ Object
Log an error message.
-
.format_string(datetime: Time.now, severity: '') ⇒ String
Format the log string.
-
.important(message:) ⇒ Object
Log an important message.
-
.kill!(message:) ⇒ Object
Kill the terminal with a message.
-
.logger ⇒ Logger
Create a logger instance if needed.
-
.message(message:) ⇒ Object
Log a message without any formatting.
-
.success(message:) ⇒ Object
Log a success message.
-
.verbose(message:) ⇒ Object
Log a verbose message.
-
.warning(message:) ⇒ Object
Log a warning message.
Class Method Details
.command(command:) ⇒ Object
Log a command message
50 51 52 |
# File 'lib/core/console/logger.rb', line 50 def command(command:) logger.info("$ #{command}".cyan) end |
.command_output(command:) ⇒ Object
Log a command output
75 76 77 78 79 80 81 82 |
# File 'lib/core/console/logger.rb', line 75 def command_output(command:) actual = encode_as_utf_8_if_possible(message: command).split("\r") .last || '' actual.split("\n").each do |cmd| prefix = cmd.include?('▸') ? '' : '▸ ' logger.info("#{prefix} #{cmd.magenta}") end end |
.crash!(message:) ⇒ Object
Crash the terminal with a message
62 63 64 |
# File 'lib/core/console/logger.rb', line 62 def crash!(message:) raise(StandardError.new, ) end |
.deprecated(message:) ⇒ Object
Log a deprecated message
44 45 46 |
# File 'lib/core/console/logger.rb', line 44 def deprecated(message:) logger.error(.to_s.deprecated) end |
.encode_as_utf_8_if_possible(message:) ⇒ String
Encode a message as UTF-8 if possible
111 112 113 114 115 116 117 118 119 |
# File 'lib/core/console/logger.rb', line 111 def encode_as_utf_8_if_possible(message:) return if .valid_encoding? return .encode(Encoding::UTF_8, Encoding::UTF_16) if .dup .force_encoding(Encoding::UTF_16) .valid_encoding? .encode(Encoding::UTF_8, invalid: :replace) end |
.error(message:) ⇒ Object
Log an error message
20 21 22 |
# File 'lib/core/console/logger.rb', line 20 def error(message:) logger.warn(.to_s.red) end |
.format_string(datetime: Time.now, severity: '') ⇒ String
Format the log string
102 103 104 105 106 |
# File 'lib/core/console/logger.rb', line 102 def format_string(datetime: Time.now, severity: '') return "#{severity} [#{datetime.strftime('%Y-%m-%d %H:%M:%S.%2N')}]: " if Globals.verbose? "[#{datetime.strftime('%H:%M:%S')}]: " end |
.important(message:) ⇒ Object
Log an important message
26 27 28 |
# File 'lib/core/console/logger.rb', line 26 def important(message:) logger.info(.to_s.blue) end |
.kill!(message:) ⇒ Object
Kill the terminal with a message
68 69 70 71 |
# File 'lib/core/console/logger.rb', line 68 def kill!(message:) error(message:) exit(1) end |
.logger ⇒ Logger
Create a logger instance if needed
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/core/console/logger.rb', line 86 def logger return @log unless @log.nil? $stdout.sync = true @log ||= ::Logger.new($stdout) @log.formatter = proc do |severity, datetime, _, | "#{format_string(datetime:, severity:)} #{}\n" end @log end |
.message(message:) ⇒ Object
Log a message without any formatting
38 39 40 |
# File 'lib/core/console/logger.rb', line 38 def (message:) logger.info(.to_s) end |
.success(message:) ⇒ Object
Log a success message
14 15 16 |
# File 'lib/core/console/logger.rb', line 14 def success(message:) logger.info(.to_s.green) end |
.verbose(message:) ⇒ Object
Log a verbose message
56 57 58 |
# File 'lib/core/console/logger.rb', line 56 def verbose(message:) logger.debug(.to_s) if Globals.verbose? end |
.warning(message:) ⇒ Object
Log a warning message
32 33 34 |
# File 'lib/core/console/logger.rb', line 32 def warning(message:) logger.info(.to_s.yellow) end |