Class: Larrow::Runner::Logger
- Inherits:
-
Object
- Object
- Larrow::Runner::Logger
- Defined in:
- lib/larrow/runner/logger.rb
Overview
usage: logger = Larrow::Runner::Logger filename logger.level(3).color(‘red’).info ‘hello’ logger.level(3).title ‘hello’ logger.level(3).detail ‘hello’
Instance Method Summary collapse
- #color(color) ⇒ Object
- #detail(msg) ⇒ Object
- #err(msg) ⇒ Object
- #info(msg) ⇒ Object
-
#initialize(logger, level: nil, color: 'green') ⇒ Logger
constructor
A new instance of Logger.
- #level(level) ⇒ Object
- #nocolor ⇒ Object
- #title(msg) ⇒ Object
- #wrap_color(msg) ⇒ Object
Constructor Details
#initialize(logger, level: nil, color: 'green') ⇒ Logger
Returns a new instance of Logger.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/larrow/runner/logger.rb', line 10 def initialize logger, level:nil, color:'green' @inner_logger = if logger.is_a? ::Logger logger else ::Logger.new logger end @inner_logger.formatter = proc do |_severity, datetime, _progname, msg| "[#{datetime.strftime('%H:%M:%S')}] #{msg}\n" end @level = level @color = color end |
Instance Method Details
#color(color) ⇒ Object
31 32 33 34 |
# File 'lib/larrow/runner/logger.rb', line 31 def color color return self if RunOption.key? :nocolor # skip color when no color Logger.new @inner_logger, level: @level, color: color end |
#detail(msg) ⇒ Object
46 47 48 |
# File 'lib/larrow/runner/logger.rb', line 46 def detail msg color('magenta').info msg end |
#err(msg) ⇒ Object
50 51 52 |
# File 'lib/larrow/runner/logger.rb', line 50 def err msg color('red').info msg end |
#info(msg) ⇒ Object
36 37 38 39 40 |
# File 'lib/larrow/runner/logger.rb', line 36 def info msg indent = " " * (@level || 0) wrapped = wrap_color msg @inner_logger.info "#{indent}#{wrapped}" end |
#level(level) ⇒ Object
27 28 29 |
# File 'lib/larrow/runner/logger.rb', line 27 def level level Logger.new @inner_logger, level: level, color: @color end |
#nocolor ⇒ Object
23 24 25 |
# File 'lib/larrow/runner/logger.rb', line 23 def nocolor @color = nil end |
#title(msg) ⇒ Object
42 43 44 |
# File 'lib/larrow/runner/logger.rb', line 42 def title msg color('yellow').info msg end |
#wrap_color(msg) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/larrow/runner/logger.rb', line 54 def wrap_color msg return msg if @color.nil? code = case @color.downcase when 'black' then '30' when 'red' then '31' when 'green' then '32' when 'yellow' then '33' when 'blue' then '34' when 'magenta' then '35' when 'cyan' then '36' when 'white' then '37' end "\033[#{code}m#{msg}\033[0m" end |