Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 Ruby 将一些输出实时记录到文件中。我希望能够在日志文件上执行 tail -f 并观察输出被写入。目前,只有在我停止 ruby 脚本时才会写入文件。我正在尝试做的事情似乎很简单。
我创建日志文件
log = File.open(logFileName, "a")
我后来写给它使用:
log.puts "#{variable}"
同样,创建了日志文件并且其中包含正确的条目,但只有在我停止运行脚本后。我需要跟踪日志文件并实时查看。
提前致谢!
通常文件输入和输出在一定程度上被缓冲。您可以通过翻转标志来禁用此行为:
log.sync = true
flush这通过在每次写入后强制执行操作来禁用缓冲。启用该功能后,诸如此类tail -f的程序可以实时读取数据。
flush
tail -f