在Java中最有效的方式来查看/轮询日志文件 [英] Most efficient way to tail/poll a log file in Java

查看:461
本文介绍了在Java中最有效的方式来查看/轮询日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多方法可以使用创建一个程序,只需调用Unix tail 命令,但是问题是这个进程必须安装在Unix机器尾部安装。另一个选择是只读取文件,一行一行,直到没有更多的行,睡一段时间,然后尝试读取下一行和重复。

现在的问题是如何去查询正在被写入的文件。 AFAIK没有办法在Java中获取异步通知,当文件被修改,因为这是太低级别(来自操作系统)。我也知道没有支持这个的库。





<如果你可以使用Java 7,那么有File Watcher,你可以实现你所需要的东西。

解决方案 >
文件通知



对于Java 6(以及之前的版本),您将不得不使用Apache Commons VFS( File Monitor) lib来创建你自己的文件监视器。如果你不能去Java 7或使用第三方库(例如commons或 jnotify )你将不得不使用轮询线程写一个文件看门狗你的自我。

There are many approaches, one could use create a Program and just call the Unix tail command, however the problem with this is that the process must be installed on a Unix machine with tail installed.

The other option is to just read the file, line by line until there are no more lines, sleep for a period of time and then try to read the next line and repeat.

The question is what/how do I go about polling a file which is actively being written to. AFAIK there is no way in Java to get async notifications when a file has been modified as this is too low level (something that comes from the OS). I am also aware of no libs which support this.

I was thinking a better approach may be to sleep/poll on the modified date of a file..?

Any solutions?

解决方案

If you can use Java 7 there are File Watchers and you could implement what you need.
File Notifications

For Java 6 (and previous) you would have to use Apache Commons VFS (File Monitor) lib to create your own file watcher.

If you can't go to Java 7 or use third party libs (e.g commons or jnotify) you would have to write a file watchdog your self using polling threads.

这篇关于在Java中最有效的方式来查看/轮询日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆