Class: BBK::Utils::LogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/bbk/utils/log_formatter.rb

Constant Summary collapse

FORMAT =
"%5s [%sUTC #%d] (%s)[%s]: %s\n"

Instance Method Summary collapse

Constructor Details

#initialize(tags: []) ⇒ LogFormatter

Returns a new instance of LogFormatter.



11
12
13
14
# File 'lib/bbk/utils/log_formatter.rb', line 11

def initialize(tags: [])
  super()
  @tags = tags
end

Instance Method Details

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



16
17
18
19
20
# File 'lib/bbk/utils/log_formatter.rb', line 16

def call(severity, time, progname, msg)
  line = msg2str(msg).gsub("\n", '\\n')
  line = "#{build_tags_text}#{line}"
  format(FORMAT, severity, format_datetime(time.utc), Process.pid, progname, thread_id, line)
end

#thread_idObject



22
23
24
25
26
27
# File 'lib/bbk/utils/log_formatter.rb', line 22

def thread_id
  [
    Thread.current.object_id.to_s,
    Thread.current.name || thread_name_from_main
  ].compact.join('@')
end

#thread_name_from_mainObject



29
30
31
32
33
# File 'lib/bbk/utils/log_formatter.rb', line 29

def thread_name_from_main
  if Thread.main.name
    Thread.current[:bbk_thread_id] ||= "#{Thread.main.name}-#{Thread.current.object_id}"
  end
end