Java实时读取日志文件

发布于 2024-11-05 05:41:43 字数 279 浏览 8 评论 0原文

我正在用 Java 编写一个 cod4 服务器控制器(我知道那里有非常好的服务器控制器,但我想从中学习)。现在我想根据日志文件中的条目采取特定操作,该文件由 cod 经常更新,并且该文件可能会变得非常大。现在,我如何高效地每隔一秒左右仅读​​取文件已更改的部分?

或者有没有办法将日志文件中更改的所有内容实时发送到 Java?(我读过一些有关管道的内容)。服务器在linux上运行。日志文件不需要仍然保存在同一位置,因为一切都应该通过 Java,我可以用它来保存它。

大约一两秒的延迟是可以接受的,但不能再长了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2024-11-12 05:41:44

当您阅读日志文件时,您可以在没有更多条目时暂停,并稍后继续处理。该进程将在写入文件时继续运行,并且只会读取附加到末尾的附加行。

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
  }
}

注意:如果文件被关闭并滚动,这可能不起作用,你必须做一些更复杂的事情来处理这个问题。

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.

夏雨凉 2024-11-12 05:41:44

您可以使用 RandomAccessFile。您可以像这样存储指向最后一个字节的指针:

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;
}

...其中 processChunk 是处理文件新输入的方法。

这离卓越还很远,但我想你已经明白了。

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.

总攻大人 2024-11-12 05:41:43

也许您可以执行“tail -f logfile.txt”子进程并监视输出流?

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文