Sha256: 70e84c3ef510977d57236e8d309d14479053a51ad673dde96dd7db1bcda1f187

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'file-tail'

module Spanx
  module Actor
    class LogReader
      attr_accessor :files, :queue, :whitelist, :threads

      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

      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

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

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

    class File < ::File
      include ::File::Tail
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spanx-0.3.0 lib/spanx/actor/log_reader.rb