Class: Spanx::Actor::LogReader

Inherits:
Object
  • Object
show all
Defined in:
lib/spanx/actor/log_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, queue, interval = 1, whitelist = nil) ⇒ LogReader

Returns a new instance of LogReader.



8
9
10
11
12
13
14
15
16
17
# File 'lib/spanx/actor/log_reader.rb', line 8

def initialize files, queue, interval = 1, whitelist = nil
  @files = Array(files).uniq.map { |file| Spanx::Actor::File.new(file) }
  @files.each do |file|
    file.interval = interval
    file.backward(0)
  end
  @whitelist = whitelist
  @queue = queue
  @threads = []
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



6
7
8
# File 'lib/spanx/actor/log_reader.rb', line 6

def files
  @files
end

#queueObject

Returns the value of attribute queue.



6
7
8
# File 'lib/spanx/actor/log_reader.rb', line 6

def queue
  @queue
end

#threadsObject

Returns the value of attribute threads.



6
7
8
# File 'lib/spanx/actor/log_reader.rb', line 6

def threads
  @threads
end

#whitelistObject

Returns the value of attribute whitelist.



6
7
8
# File 'lib/spanx/actor/log_reader.rb', line 6

def whitelist
  @whitelist
end

Instance Method Details

#extract_ip(line) ⇒ Object



37
38
39
40
# File 'lib/spanx/actor/log_reader.rb', line 37

def extract_ip line
  matchers = line.match(/^((\d{1,3}\.?){4})/)
  matchers[1] unless matchers.nil?
end

#read(file) ⇒ Object



31
32
33
34
35
# File 'lib/spanx/actor/log_reader.rb', line 31

def read file
  file.tail do |line|
    yield extract_ip(line) unless whitelist && whitelist.match?(line)
  end
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spanx/actor/log_reader.rb', line 19

def run
  files.each_with_index do |file, i|
    threads << Thread.new do
      Thread.current[:name] = "log_reader.#{i}"
      Logger.log "tailing the log file #{file.path}...."
      self.read(file) do |line|
        queue << [line, Time.now.to_i] if line
      end
    end
  end
end