Class: QueuedLogger

Inherits:
Logger show all
Defined in:
lib/queued_logger.rb

Instance Attribute Summary

Attributes inherited from Logger

#formatter

Instance Method Summary collapse

Methods inherited from Logger

#datetime_format, #datetime_format=, #old_datetime_format, #old_datetime_format=, #old_formatter, #silence

Constructor Details

#initialize(*args) ⇒ QueuedLogger

Returns a new instance of QueuedLogger.



4
5
6
7
8
# File 'lib/queued_logger.rb', line 4

def initialize(*args)
  super(*args)
  @queue = []
  @emitted = false
end

Instance Method Details

#dequeue(level = nil, message = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/queued_logger.rb', line 27

def dequeue(level=nil, message=nil)
  if queued?
    @queue.clear
  elsif level and message
    send("%s_without_queue"%level, message) if @emitted
  end
  @emitted = false
end

#enqueue(level, message) ⇒ Object



22
23
24
25
# File 'lib/queued_logger.rb', line 22

def enqueue(level, message)
  @queue << [level, message]
  @emitted = false
end

#process_queueObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/queued_logger.rb', line 10

def process_queue
  if queued?
    while true
      entry = @queue.shift or break
      severity, message = entry
      raw_method = "#{severity}_without_queue"
      severity_i = self.class.send(:const_get, severity.to_s.upcase)
      send(raw_method, message)
    end
  end
end

#queued?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/queued_logger.rb', line 36

def queued?
  ! @queue.empty?
end