Class: Rundock::Logger::Formatter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Formatter

Returns a new instance of Formatter.



38
39
40
41
42
43
44
# File 'lib/rundock/logger.rb', line 38

def initialize(*args)
  super
  @indent_depth = 0
  @buffer = []
  @rec = false
  @lock = false
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



36
37
38
# File 'lib/rundock/logger.rb', line 36

def buffer
  @buffer
end

#colorObject

Returns the value of attribute color.



32
33
34
# File 'lib/rundock/logger.rb', line 32

def color
  @color
end

#coloredObject

Returns the value of attribute colored.



30
31
32
# File 'lib/rundock/logger.rb', line 30

def colored
  @colored
end

#date_headerObject

Returns the value of attribute date_header.



35
36
37
# File 'lib/rundock/logger.rb', line 35

def date_header
  @date_header
end

#indent_depthObject

Returns the value of attribute indent_depth.



31
32
33
# File 'lib/rundock/logger.rb', line 31

def indent_depth
  @indent_depth
end

#short_headerObject

Returns the value of attribute short_header.



34
35
36
# File 'lib/rundock/logger.rb', line 34

def short_header
  @short_header
end

#show_headerObject

Returns the value of attribute show_header.



33
34
35
# File 'lib/rundock/logger.rb', line 33

def show_header
  @show_header
end

Instance Method Details

#add_indentObject



64
65
66
# File 'lib/rundock/logger.rb', line 64

def add_indent
  @indent_depth += 1
end

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



46
47
48
49
50
51
52
53
54
55
# File 'lib/rundock/logger.rb', line 46

def call(severity, datetime, progname, msg)
  out = formatted_message(severity, datetime, progname, msg)
  @buffer << LogEntity.new(severity, datetime, progname, msg, indent_depth, self) if @rec

  if colored
    colorize(out, severity)
  else
    out
  end
end

#flushObject



80
81
82
83
84
85
# File 'lib/rundock/logger.rb', line 80

def flush
  return nil if @lock
  ret = @buffer.dup
  @buffer.clear
  ret
end

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rundock/logger.rb', line 103

def formatted_message(severity, datetime, progname, msg)
  out = if !@show_header
          "%s\n" % [msg2str(msg)]
        elsif !@date_header
          "%5s: %s%s\n" % [
            severity,
            ' ' * 2 * indent_depth,
            msg2str(msg)
          ]
        elsif @short_header
          "%s: %s%s\n" % [severity[0, 1], ' ' * 2 * indent_depth, msg2str(msg)]
        else
          "[%s] %5s: %s%s\n" % [
            datetime.strftime('%Y-%m-%dT%H:%M:%S.%L'),
            severity,
            ' ' * 2 * indent_depth,
            msg2str(msg)
          ]
        end

  out
end

#indentObject



57
58
59
60
61
62
# File 'lib/rundock/logger.rb', line 57

def indent
  add_indent
  yield
ensure
  reduce_indent
end

#new_color(code) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rundock/logger.rb', line 72

def new_color(code)
  prev_color = @color
  @color = code
  yield
ensure
  @color = prev_color
end

#off_recObject



91
92
93
# File 'lib/rundock/logger.rb', line 91

def off_rec
  @rec = false unless @lock
end

#on_recObject



87
88
89
# File 'lib/rundock/logger.rb', line 87

def on_rec
  @rec = true unless @lock
end

#rec_lockObject



95
96
97
# File 'lib/rundock/logger.rb', line 95

def rec_lock
  @lock = true
end

#rec_unlockObject



99
100
101
# File 'lib/rundock/logger.rb', line 99

def rec_unlock
  @lock = false
end

#reduce_indentObject



68
69
70
# File 'lib/rundock/logger.rb', line 68

def reduce_indent
  @indent_depth -= 1 if @indent_depth > 0
end