class DopCommon::ThreadContextLogger

Public Class Methods

new(log_path, contexts, all = true) click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 11
def initialize(log_path, contexts, all = true)
  @log_path = log_path
  @contexts = contexts
  @all      = all
  @mutex    = Mutex.new
  @loggers  = {}
  @threads  = {}

  FileUtils.mkdir_p(@log_path)
  create
end

Public Instance Methods

cleanup() click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 30
def cleanup
  @mutex.synchronize do
    @contexts.each{|context| remove(context)}
  end
end
create() click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 23
def create
  @mutex.synchronize do
    add('all') if @all
    @contexts.each{|context| add(context)}
  end
end
current_log_file() click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 42
def current_log_file
  context = @threads[Thread.current.object_id.to_s]
  log_file(context)
end
log_context=(context) click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 36
def log_context=(context)
  @mutex.synchronize do
    @threads[Thread.current.object_id.to_s] = context
  end
end

Private Instance Methods

add(context) click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 53
def add(context)
  logger = Logger.new(log_file(context))
  if context == 'all'
    logger.formatter = Logger::Formatter.new
  else
    logger.formatter = formatter(context)
  end
  logger.level = ::Logger.const_get(DopCommon.config.log_level.upcase)

  @loggers[context] = logger
  DopCommon.add_log_junction(logger)
end
formatter(context) click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 71
def formatter(context)
  orig_formatter = Logger::Formatter.new
  Proc.new do |severity, datetime, progname, msg|
    @mutex.synchronize do
      if context == @threads[Thread.current.object_id.to_s]
        orig_formatter.call(severity, datetime, progname, msg)
      else
        nil
      end
    end
  end
end
log_file(context) click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 49
def log_file(context)
  File.join(@log_path, context)
end
remove(context) click to toggle source
# File lib/dop_common/thread_context_logger.rb, line 66
def remove(context)
  logger = @loggers[context]
  DopCommon.remove_log_junction(logger)
end