Class: Flor::Logger

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

Overview

No log rotation, just dump to stdout (or stderr), see 12factor.net/logs

Defined Under Namespace

Classes: FileOut, NoOut, Out, StdOut

Constant Summary collapse

LEVELS_I =

NB: logger configuration entries start with “log_”

%w[ DEBUG INFO WARN ERROR FATAL UNKNOWN ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit) ⇒ Logger

Returns a new instance of Logger.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flor/unit/logger.rb', line 17

def initialize(unit)

  @unit = unit
  @unit.hooker.add('logger', self)

  @out = Flor::Logger::Out.prepare(@unit)

  @uni = @unit.identifier

  self.level = @unit.conf['log_level'] || 1
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

Instance Method Details

#db_log(level, msg) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/flor/unit/logger.rb', line 111

def db_log(level, msg)

  return unless @unit.conf['log_sto']
#return unless msg.match(/flor_timers/)
#p Time.now

  _c = Flor.colours(out: @out)

  #m = msg.match(/ (INSERT|UPDATE) .+ (0?[xX]'?[a-fA-F0-9]+'?)/)
  #msg = msg.sub(m[2], "#{m[2][0, 14]}(...len#{m[2].length})") if m
    #
    # with a fat blob, may lead to memory problems very quickly, hence:
    #
  msg = summarize_blob(msg)

  msg = "#{_c.dg}#{msg}"
  msg = msg.gsub(/\b(INSERT|UPDATE)\b/) { |m| "#{_c.rs}#{m}#{_c.dg}" }

  tim = Time.now.utc.strftime('%T.%L') # HH:MM:SS.123
  dbi = @unit.storage.db.object_id.to_s(16)
  tid = Thread.current.object_id.to_s(16)
  lvl = level.upcase

  @out.puts(
    "#{_c.blg}sto#{_c.rs} " +
    "#{tim} #{_c.dg}db#{dbi} th#{tid} " +
    "#{_c.rs}#{lvl}#{_c.rs} #{msg}#{_c.rs}")
end

#debug(*m) ⇒ Object



51
# File 'lib/flor/unit/logger.rb', line 51

def debug(*m); log(:debug, *m); end

#error(*m) ⇒ Object



54
# File 'lib/flor/unit/logger.rb', line 54

def error(*m); log(:error, *m); end

#fatal(*m) ⇒ Object



55
# File 'lib/flor/unit/logger.rb', line 55

def fatal(*m); log(:fatal, *m); end

#info(*m) ⇒ Object



52
# File 'lib/flor/unit/logger.rb', line 52

def info(*m); log(:info, *m); end

#log(level, *elts) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flor/unit/logger.rb', line 58

def log(level, *elts)

  lvl = level.to_s.upcase
  lvi = LEVELS_I.index(lvl)
  return if lvi < @level

  n = Time.now.utc
  stp = Flor.tstamp(n)

  dbi =
    case @unit.storage.db.to_s
    when /SQLite/ then @unit.storage.db.uri
    else ''
    end
  dbi = ' ' + dbi if dbi.length > 0

  txt = elts.collect(&:to_s).join(' ')

  err = find_err(elts)

  head = "#{stp} #{@uni}#{dbi} #{lvl} "

  if err
    dig = lvl[0, 1] + Digest::MD5.hexdigest(head + txt)[0, 4]
    @out.puts(head + dig + ' ' + txt)
    err.backtrace.each { |lin| @out.puts("  #{dig} #{@uni} #{lin}") }
  else
    @out.puts(head + txt)
  end
end

#log_err(executor, message, opts = {}) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/flor/unit/logger.rb', line 199

def log_err(executor, message, opts={})

  return unless @unit.conf['log_err']

  s = Flor.msg_to_detail_s(executor, message, opts.merge(flag: true))
  @out.puts(s) if s
end

#log_run_end(executor, tstamp, duration) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/flor/unit/logger.rb', line 166

def log_run_end(executor, tstamp, duration)

  return unless @unit.conf['log_run']

  execution = executor.execution
  exid = executor.exid

  _c = Flor.colours(out: @out)
  s = StringIO.new

  s << _c.dg
  s << "    |   run ends #{self.class} #{self.object_id} #{exid}"
  #s << "\n    |   "; s << Flor.to_dnc(started: tstamp, took: duration)
  s << "\n    |   "; s << Flor.to_dnc(started: tstamp, took: duration)
  s << "\n    |   "; s << Flor.to_dnc(
    thread: Thread.current.object_id,
    consumed: executor.consumed.count,
    traps: executor.traps.count)
  s << "\n    |   "; s << Flor.to_dnc(
    #own_traps: @traps.reject { |t| t.texid == nil }.size, # FIXME
    counters: execution['counters'],
    nodes: execution['nodes'].size,
    execution_size: size_to_s(execution['size']))
  if @unit.archive
    s << "\n    |   "
    s << Flor.to_dnc(archive_size: (@unit.archive[exid].size rescue '???'))
  end
  s << "\n    \\--- ."
  s << _c.rs

  @out.puts(s.string)
end

#log_run_start(executor) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/flor/unit/logger.rb', line 145

def log_run_start(executor)

  return unless @unit.conf['log_run']

  execution = executor.execution
  _c = Flor.colours(out: @out)
  s = StringIO.new

  s << _c.dg
  s << "    /--- #{_c.lg}run starts#{_c.dg} "
  s << "#{executor.class} #{executor.object_id} #{execution['exid']}"
  s << "\n    |   "; s << Flor.to_dnc(thread: Thread.current.object_id)
  s << "\n    |   "; s << Flor.to_dnc(
    counters: execution['counters'],
    nodes: execution['nodes'].size,
    execution_size: size_to_s(execution['size']))
  s << _c.rs

  @out.puts(s.string)
end

#log_src(source, opts, log_opts = {}) ⇒ Object



207
208
209
210
211
212
# File 'lib/flor/unit/logger.rb', line 207

def log_src(source, opts, log_opts={})

  return unless @unit.conf['log_src']

  @out.puts(Flor.src_to_s(source, opts, log_opts))
end

#log_tree(tree, nid = '0', opts = {}) ⇒ Object



214
215
216
217
218
219
# File 'lib/flor/unit/logger.rb', line 214

def log_tree(tree, nid='0', opts={})

  return unless @unit.conf['log_tree']

  @out.puts(Flor.tree_to_s(tree, nid, opts.merge(out: @out)))
end

#notify(executor, msg) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flor/unit/logger.rb', line 89

def notify(executor, msg)

  if msg['rewritten'] && @unit.conf['log_tree_rw']

    @out <<
      Flor.to_compact_tree_s(
        msg['rewritten'], msg['nid'],
        ind: 6, title: "rewrote #{msg['exid']} #{msg['nid']}")
    @out << "\n" <<
      Flor.to_compact_tree_s(
        msg['tree'], msg['nid'],
        ind: 6, title: "into #{msg['exid']} #{msg['nid']}",
        close: true)
  end

  @out.puts(
    Flor.message_to_one_line_s(executor, msg, out: @out)
  ) if @unit.conf['log_msg'] && msg['point'] != 'end'

  [] # we're only logging, do not queue further messages
end

#optsObject



29
# File 'lib/flor/unit/logger.rb', line 29

def opts; { consumed: true }; end

#shutdownObject



31
32
33
34
# File 'lib/flor/unit/logger.rb', line 31

def shutdown

  @out.close
end

#size_to_s(s) ⇒ Object



140
141
142
143
# File 'lib/flor/unit/logger.rb', line 140

def size_to_s(s)

  "%.2fk" % (s.to_f / 1024)
end

#unknown(*m) ⇒ Object



56
# File 'lib/flor/unit/logger.rb', line 56

def unknown(*m); log(:unknown, *m); end

#warn(*m) ⇒ Object



53
# File 'lib/flor/unit/logger.rb', line 53

def warn(*m); log(:warn, *m); end