Class: Urbivore::Logger

Inherits:
Object
  • Object
show all
Extended by:
Interface
Defined in:
lib/urbivore/logger.rb

Constant Summary collapse

DEFAULT_SLEEP_TIME =

worker constants default to 250ms sleep (more? less?)

(1.0/4.0)
DEFAULT_TOTAL_WORKERS =
1
DEFAULT_BATCH_SIZE =
5

Constants included from Interface

Interface::STANDARD_LEVELS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Interface

add, append_message, available_levels, current_levels, default_level, default_levels, default_logger, generate_message_object, levels_by_minimum, levels_by_subset

Class Attribute Details

.configured_levelObject (readonly)

Returns the value of attribute configured_level.



6
7
8
# File 'lib/urbivore/logger.rb', line 6

def configured_level
  @configured_level
end

.configured_levelsObject (readonly)

Returns the value of attribute configured_levels.



6
7
8
# File 'lib/urbivore/logger.rb', line 6

def configured_levels
  @configured_levels
end

Class Method Details

.batch_sizeObject



55
56
57
# File 'lib/urbivore/logger.rb', line 55

def batch_size
  @batch_size || DEFAULT_BATCH_SIZE
end

.batch_size=(batch_size) ⇒ Object



76
77
78
79
80
81
# File 'lib/urbivore/logger.rb', line 76

def batch_size=(batch_size)
  unless batch_size.is_a?(Fixnum) || !batch_size
    raise Urbivore::Exceptions::ConfigurationError.new("Log.batch_size must be a number")
  end
  @batch_size = batch_size
end

.loggerObject



35
36
37
# File 'lib/urbivore/logger.rb', line 35

def logger
  self
end

.process(entry) ⇒ Object



20
21
22
# File 'lib/urbivore/logger.rb', line 20

def process(entry)
  processor.process(entry)
end

.processor(processor = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/urbivore/logger.rb', line 24

def processor(processor = nil)
  if processor
    unless processor.respond_to?(:process)
      raise Urbivore::Exceptions::ConfigurationError.new("Processor must respond_to?(:process)")
    end
    @processor = processor
  else
    @processor ||= Urbs::BasicProcessor
  end
end

.queueObject



43
44
45
# File 'lib/urbivore/logger.rb', line 43

def queue
  @queue ||= Queue.new
end

.restart_logging(halt = false) ⇒ Object

THREAD SAFETY…



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/urbivore/logger.rb', line 84

def restart_logging(halt = false)
  Thread.exclusive do
    reset_state
    if halt
      true
    else
      @keep_going   = true
      total_workers.times do
        workers << Thread.new do
          while @keep_going
            sleep sleep_time
            entries   = []
            batch_size.times do 
              entries << queue.pop
            end
            entries.each { |entry| process(entry) }
          end
        end
      end
    end
  end
end

.sleep_timeObject



47
48
49
# File 'lib/urbivore/logger.rb', line 47

def sleep_time
  @sleep_time || DEFAULT_SLEEP_TIME
end

.sleep_time=(sleep_time) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/urbivore/logger.rb', line 59

def sleep_time=(sleep_time)
  unless sleep_time.is_a?(Numeric) || !sleep_time
    raise Urbivore::Exceptions::ConfigurationError.new("Log.sleep_time must be a number")
  end

  # we should figure out whether access to this instance var 
  # should be locked, in this method and the getter...
  @sleep_time = sleep_time
end

.submit(entry) ⇒ Object

use this to enqueue entries



15
16
17
18
# File 'lib/urbivore/logger.rb', line 15

def submit(entry)
  # any prep work on the object goes here...
  queue.push(entry)
end

.total_workersObject



51
52
53
# File 'lib/urbivore/logger.rb', line 51

def total_workers
  @total_workers || DEFAULT_TOTAL_WORKERS
end

.total_workers=(total_workers) ⇒ Object



69
70
71
72
73
74
# File 'lib/urbivore/logger.rb', line 69

def total_workers=(total_workers)
  unless total_workers.is_a?(Fixnum) || !total_workers
    raise Urbivore::Exceptions::ConfigurationError.new("Log.total_workers must be a number")
  end
  @total_workers = total_workers
end

.workersObject



39
40
41
# File 'lib/urbivore/logger.rb', line 39

def workers
  @workers ||= []
end