Skip to content

Instantly share code, notes, and snippets.

@gaiottino
Created December 18, 2010 13:19
Show Gist options
  • Save gaiottino/746500 to your computer and use it in GitHub Desktop.
Save gaiottino/746500 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'eventmachine-tail'
class Tailer < EventMachine::FileTail
def initialize(path, startpos = -1)
super(path, startpos)
@buffer = BufferedTokenizer.new
end
def receive_data(data)
@buffer.extract(data).each do |line|
end
end
end
class Watcher < EventMachine::FileGlobWatch
def initialize(pathglob, interval=5)
super(pathglob, interval)
@tailers = {}
end
def file_deleted(path)
tailer = @tailers.delete(path)
p tailer
tailer.close if tailer
puts "Removed: #{path}"
end
def file_found(path)
tailer = Tailer.new(path)
p tailer
@tailers[path] = tailer
puts "Found: #{path}"
end
end
EM.run do
Watcher.new("/mnt/var/log/apache2/access*")
puts "Tailing started"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment