Class: Rails::Rack::LogTailer

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

Constant Summary collapse

EnvironmentLog =
"#{File.expand_path(Rails.root)}/log/#{Rails.env}.log"

Instance Method Summary collapse

Constructor Details

#initialize(app, log = nil) ⇒ LogTailer

Returns a new instance of LogTailer.



6
7
8
9
10
11
12
13
14
# File 'lib/rails/rack/log_tailer.rb', line 6

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

  path = Pathname.new(log || EnvironmentLog).cleanpath
  @cursor = ::File.size(path)
  @last_checked = Time.now.to_f

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

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
# File 'lib/rails/rack/log_tailer.rb', line 16

def call(env)
  response = @app.call(env)
  tail_log
  response
end

#tail_logObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rails/rack/log_tailer.rb', line 22

def tail_log
  @file.seek @cursor

  mod = @file.mtime.to_f
  if mod > @last_checked
    contents = @file.read
    @last_checked = mod
    @cursor += contents.size
    $stdout.print contents
  end
end