class Tengine::Support::NamedLogger

Constants

LOG_FORMAT
WHITE

Attributes

name[R]

Public Class Methods

new(name, *args, &block) click to toggle source
Calls superclass method
# File lib/tengine/support/named_logger.rb, line 5
def initialize(name, *args, &block)
  super(*args, &block)
  @name = name
  @mutex = Mutex.new
  @thread_nums ||= {}
  thread_num(Thread.main.object_id) # => 0 のはず
  self.formatter = method(:format_with_color)
end

Public Instance Methods

format_with_color(severity, datetime, progname, message) click to toggle source
# File lib/tengine/support/named_logger.rb, line 27
def format_with_color(severity, datetime, progname, message)
  tn = thread_num
  # http://d.hatena.ne.jp/keyesberry/20101107/p1
  color_no = (tn == 0) ? WHITE : WHITE - 1 - ((tn - 1) % 6) # メインスレッドを白、それ以外を色付きに
  LOG_FORMAT % [color_no, datetime.iso8601(6), Process.pid, @name, tn, severity, message]
end
thread_num(tid = Thread.current.object_id) click to toggle source
# File lib/tengine/support/named_logger.rb, line 14
def thread_num(tid = Thread.current.object_id)
  result = @thread_nums[tid]
  return result if result
  @mutex.synchronize do
    num = @thread_nums[tid] = (@thread_nums.length)
    return num
  end
end