开发者

Java read a logfile live

I'm writing a cod4 server controller in Java(I know there are perfectly fine server controllers out there, but I want to learn from it). Now I want to 开发者_如何学编程take specific actions according to entries in a logfile, this file is updated by cod very often, and the file can get quite large. Now how do I efficiently read only the part that has changed of the file, every second or so?

Or is there a way to send everything that is changed in the logfile live to Java?(I read something about pipes). The server runs on linux. It's not needed that the logfile is still saved in the same location, since everything should go through Java I can just save it with that.

A delay of about a second or 2 is acceptable, but not any longer.


Maybe you could execute a 'tail -f logfile.txt' subprocess and monitor the output stream?

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html


While you are reading the log file, you can pause when there are no more entries, and continue processing later. The process would continue running while the file is being written to and would only read additional lines appended to the end.

BufferedReader br = ...;
String line = null;
while (true) {
  line = br.readLine();
  if (line == null) // nothing more to read, wait... 
  {
    Thread.sleep(delay);
  } else {
    process(line); // take action
  }
}

Note: if the file is closed and rolled over, this probably won't work, and you'll have to do something more sophisticated to handle that.


You could use RandomAccessFile. You could store the pointer to the last byte you have red like this:

String pathToYourFile = "/path/to/your/logfile";
long lastBytePosition = 0;
boolean shouldStop = false;
while (! shouldStop) {
    Thread.sleep(2000);
    File f = new File(pathToYourFile);
    long length = f.length();
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    byte[] buff = new byte[(int) (length - lastBytePosition)];
    raf.readFully(buff, (int) lastBytePosition, (int) (length - lastBytePosition));
    shouldStop = processChunk(buff);
    lastBytePosition = (int) length;
}

...where processChunk is a method to deal with new input from a file.

It's very far from excellence, but I think you got the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜