How to read a constantly updated log file in scala

I want to read a constantly updated file like (tail -f) in scala, I can’t use other tools like tail because I need to do extra post processing.

So how to track the exact contents of a file every time.

+4
source share
2 answers

The Apache implementation implements the tail -f implementation. http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/input/Tailer.html
With google search I found another implementation of "tail -f": https: //github.com/alaz/tailf/blob/master/src/main/scala/com/osinka/tailf/Tail.scala

+6
source

tail -f Scala, . API scala.sys.process:

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)

. , . , , .

+3

All Articles