Class: Howzit::ConsoleLogger
- Inherits:
-
Object
- Object
- Howzit::ConsoleLogger
- Defined in:
- lib/howzit/console_logger.rb
Overview
Console logging
Instance Attribute Summary collapse
-
#log_level ⇒ Object
Returns the value of attribute log_level.
Instance Method Summary collapse
-
#color_for_level(level) ⇒ String
Get color prefix for log level.
-
#debug(msg) ⇒ Object
Write a message at debug level.
-
#emoji_for_level(level) ⇒ String
Get emoji for log level.
-
#error(msg) ⇒ Object
Write a message at error level.
-
#info(msg) ⇒ Object
Write a message at info level.
-
#initialize(level = nil) ⇒ ConsoleLogger
constructor
Init the console logging object.
-
#reset_level ⇒ Integer
Get the log level from options.
-
#warn(msg) ⇒ Object
Write a message at warn level.
-
#write(msg, level = :info) ⇒ Object
Write a message to the console based on the urgency level and user’s log level setting.
Constructor Details
#initialize(level = nil) ⇒ ConsoleLogger
Init the console logging object
21 22 23 |
# File 'lib/howzit/console_logger.rb', line 21 def initialize(level = nil) @log_level = level.to_i || Howzit.[:log_level] end |
Instance Attribute Details
#log_level ⇒ Object
Returns the value of attribute log_level.
14 15 16 |
# File 'lib/howzit/console_logger.rb', line 14 def log_level @log_level end |
Instance Method Details
#color_for_level(level) ⇒ String
Get color prefix for log level
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/howzit/console_logger.rb', line 63 def color_for_level(level) case level when :debug '{d}' when :info '{c}' when :warn '{y}' when :error '{r}' else '' end end |
#debug(msg) ⇒ Object
Write a message at debug level
108 109 110 |
# File 'lib/howzit/console_logger.rb', line 108 def debug(msg) write msg, :debug end |
#emoji_for_level(level) ⇒ String
Get emoji for log level
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/howzit/console_logger.rb', line 41 def emoji_for_level(level) case level when :debug '🔍' when :info 'ℹ️' when :warn '⚠️' when :error '❌' else '' end end |
#error(msg) ⇒ Object
Write a message at error level
149 150 151 |
# File 'lib/howzit/console_logger.rb', line 149 def error(msg) write msg, :error end |
#info(msg) ⇒ Object
Write a message at info level
117 118 119 |
# File 'lib/howzit/console_logger.rb', line 117 def info(msg) write msg, :info end |
#reset_level ⇒ Integer
Get the log level from options
30 31 32 |
# File 'lib/howzit/console_logger.rb', line 30 def reset_level @log_level = Howzit.[:log_level] end |
#warn(msg) ⇒ Object
Write a message at warn level
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/howzit/console_logger.rb', line 126 def warn(msg) return unless LOG_LEVELS[:warn] >= @log_level emoji = emoji_for_level(:warn) color = color_for_level(:warn) formatted_msg = if emoji && color "#{emoji} #{color}#{msg}{x}".c else msg end begin $stderr.puts formatted_msg rescue Errno::EPIPE # Pipe closed, ignore end end |
#write(msg, level = :info) ⇒ Object
Write a message to the console based on the urgency level and user’s log level setting
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/howzit/console_logger.rb', line 85 def write(msg, level = :info) return unless LOG_LEVELS[level] >= @log_level emoji = emoji_for_level(level) color = color_for_level(level) formatted_msg = if emoji && color "#{emoji} #{color}#{msg}{x}".c else msg end begin $stderr.puts formatted_msg rescue Errno::EPIPE # Pipe closed, ignore end end |