Class: DopCommon::ThreadContextLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/dop_common/thread_context_logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(log_path, contexts, all = true) ⇒ ThreadContextLogger

Returns a new instance of ThreadContextLogger.



11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#cleanupObject



30
31
32
33
34
# File 'lib/dop_common/thread_context_logger.rb', line 30

def cleanup
  @mutex.synchronize do
    @contexts.each{|context| remove(context)}
  end
end

#createObject



23
24
25
26
27
28
# 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_fileObject



42
43
44
45
# 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) ⇒ Object



36
37
38
39
40
# 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