Class: Spanx::Actor::LogReader
- Inherits:
-
Object
- Object
- Spanx::Actor::LogReader
- Defined in:
- lib/spanx/actor/log_reader.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#threads ⇒ Object
Returns the value of attribute threads.
-
#whitelist ⇒ Object
Returns the value of attribute whitelist.
Instance Method Summary collapse
- #extract_ip(line) ⇒ Object
-
#initialize(files, queue, interval = 1, whitelist = nil) ⇒ LogReader
constructor
A new instance of LogReader.
- #read(file) ⇒ Object
- #run ⇒ Object
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
#files ⇒ Object
Returns the value of attribute files.
6 7 8 |
# File 'lib/spanx/actor/log_reader.rb', line 6 def files @files end |
#queue ⇒ Object
Returns the value of attribute queue.
6 7 8 |
# File 'lib/spanx/actor/log_reader.rb', line 6 def queue @queue end |
#threads ⇒ Object
Returns the value of attribute threads.
6 7 8 |
# File 'lib/spanx/actor/log_reader.rb', line 6 def threads @threads end |
#whitelist ⇒ Object
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 |
#run ⇒ Object
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 |