Reputation: 93
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!
Upvotes: 1
Views: 560
Reputation: 211590
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.
Upvotes: 3