Class: Itamae::Logger::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/itamae/logger.rb

Constant Summary collapse

INDENT_LENGTH =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Formatter

Returns a new instance of Formatter.



14
15
16
17
18
# File 'lib/itamae/logger.rb', line 14

def initialize(*args)
  super

  @depth = 0
end

Instance Attribute Details

#color(code) ⇒ Object

Returns the value of attribute color.



10
11
12
# File 'lib/itamae/logger.rb', line 10

def color
  @color
end

#coloredObject

Returns the value of attribute colored.



8
9
10
# File 'lib/itamae/logger.rb', line 8

def colored
  @colored
end

#depthObject

Returns the value of attribute depth.



9
10
11
# File 'lib/itamae/logger.rb', line 9

def depth
  @depth
end

Instance Method Details

#call(severity, datetime, progname, msg) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/itamae/logger.rb', line 20

def call(severity, datetime, progname, msg)
  log = "%s : %s%s\n" % ["%5s" % severity, ' ' * INDENT_LENGTH * depth , msg2str(msg)]
  if colored
    colorize(log, severity)
  else
    log
  end
end

#indentObject



44
45
46
# File 'lib/itamae/logger.rb', line 44

def indent
  @depth += 1
end

#outdentObject



48
49
50
51
# File 'lib/itamae/logger.rb', line 48

def outdent
  @depth -= 1
  @depth = 0 if @depth < 0
end

#with_indentObject



29
30
31
32
33
34
# File 'lib/itamae/logger.rb', line 29

def with_indent
  indent
  yield
ensure
  outdent
end

#with_indent_if(condition, &block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/itamae/logger.rb', line 36

def with_indent_if(condition, &block)
  if condition
    with_indent(&block)
  else
    block.call
  end
end