1

I am trying to log some output to a file in realtime using Ruby. I would like to be able to do a tail -f on the log file and watch the output get written. At the moment the file only gets written to once I stop the ruby script. What I am trying to do seems straight forward.

I create the logfile

log = File.open(logFileName, "a")

I later write to it using:

log.puts "#{variable}"

Again, the log file gets created and the correct entries are in it but only once I have stopped the script from running. I need to tail the log file and see in realtime.

Thanks in advance!

Rj01
  • 93
  • 1
  • 7

1 Answers1

3

Normally file input and output is buffered to a degree. You can disable this behaviour by flipping a flag:

log.sync = true

This disables buffering by forcing a flush operation after each write. With that enabled, programs like tail -f can read the data in real-time.

tadman
  • 208,517
  • 23
  • 234
  • 262