Class: Rails::Rack::LogTailer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/rack/log_tailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, log = nil) ⇒ LogTailer

Returns a new instance of LogTailer.



4
5
6
7
8
9
10
11
# File 'lib/rails/rack/log_tailer.rb', line 4

def initialize(app, log = nil)
  @app = app

  path = Pathname.new(log || "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath
  @cursor = ::File.size(path)

  @file = ::File.open(path, 'r')
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
# File 'lib/rails/rack/log_tailer.rb', line 13

def call(env)
  response = @app.call(env)
  tail!
  response
end

#tail!Object



19
20
21
22
23
24
25
26
27
# File 'lib/rails/rack/log_tailer.rb', line 19

def tail!
  @file.seek @cursor

  if !@file.eof?
    contents = @file.read
    @cursor = @file.tell
    $stdout.print contents
  end
end