如何在scala中读取持续更新的日志文件
我想在scala中读取一个持续更新的文件(tail-f),我不能使用其他工具,比如tail,因为我需要对记录进行一些额外的处理如何在scala中读取持续更新的日志文件,scala,Scala,我想在scala中读取一个持续更新的文件(tail-f),我不能使用其他工具,比如tail,因为我需要对记录进行一些额外的处理 因此,如何每次都跟踪精确的文件内容。在Apache commons中有一个“tail-f”的实现 通过谷歌搜索,我发现了“tail-f”的另一个实现:您可以随时从Scala调用tail-f,然后在那里进行额外的处理。使用scala.sys.processAPI: import scala.sys.process._ def someProcessing(line: S
因此,如何每次都跟踪精确的文件内容。在Apache commons中有一个“tail-f”的实现
通过谷歌搜索,我发现了“tail-f”的另一个实现:您可以随时从Scala调用
tail-f
,然后在那里进行额外的处理。使用scala.sys.process
API:
import scala.sys.process._
def someProcessing(line: String): Unit = {
// do whatever you want with that line
print("[just read this line] ")
println(line)
}
// the file to read
val file = "mylogfile.txt"
// the process to start
val tail = Seq("tail", "-f", file)
// continuously read lines from tail -f
tail.lineStream.foreach(someProcessing)
// careful: this never returns (unless tail is externally killed)
编辑:这样做的一个优点是不涉及轮询。但作为交换,这可能会以不可中断的方式阻止调用线程