2008-12-18 17:42:06 -05:00
|
|
|
module Rails
|
|
|
|
module Rack
|
|
|
|
class LogTailer
|
|
|
|
def initialize(app, log = nil)
|
|
|
|
@app = app
|
|
|
|
|
2012-01-18 15:12:37 -05:00
|
|
|
path = Pathname.new(log || "#{::File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath
|
2008-12-18 17:42:06 -05:00
|
|
|
|
2012-01-18 15:12:37 -05:00
|
|
|
@cursor = @file = nil
|
2013-11-01 05:39:06 -04:00
|
|
|
if ::File.exist?(path)
|
2012-01-18 15:12:37 -05:00
|
|
|
@cursor = ::File.size(path)
|
|
|
|
@file = ::File.open(path, 'r')
|
|
|
|
end
|
2008-12-18 17:42:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
response = @app.call(env)
|
2010-01-15 04:46:30 -05:00
|
|
|
tail!
|
2008-12-18 17:42:06 -05:00
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2010-01-15 04:46:30 -05:00
|
|
|
def tail!
|
2012-01-18 15:12:37 -05:00
|
|
|
return unless @cursor
|
2008-12-18 17:42:06 -05:00
|
|
|
@file.seek @cursor
|
|
|
|
|
2010-10-16 23:12:19 -04:00
|
|
|
unless @file.eof?
|
2008-12-18 17:42:06 -05:00
|
|
|
contents = @file.read
|
2010-07-28 20:34:04 -04:00
|
|
|
@cursor = @file.tell
|
2008-12-18 17:42:06 -05:00
|
|
|
$stdout.print contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|