简体   繁体   English

Java尾日志文件并在jsp中显示

[英]Java tail log file and display it in jsp

I would like to display log file contents in a jsp page (as part of a web application functionality - cannot use another standalone webapp). 我想在jsp页面中显示日志文件的内容(作为Web应用程序功能的一部分-不能使用另一个独立的Webapp)。 I am looking for something similar to the unix tail -f functionality. 我正在寻找类似于unix tail -f功能的东西。 I looked at Apache Commons Tailer and TailerListenerAdapter. 我看了Apache Commons Tailer和TailerListenerAdapter。 However, the TailerListenerAdapter has a method handle(String line) which is not useful if you would like to see the output in a web browser. 但是,TailerListenerAdapter具有方法handle(String line),如果您想在Web浏览器中查看输出,则该方法没有用。 There are other solutions such as reading the file and then polling the file for new contents. 还有其他解决方案,例如读取文件,然后轮询文件以获取新内容。 But it would involve too many IO operations in a loop. 但这会在循环中涉及过多的IO操作。 Ideally I would want to tail the contents (say 2000 lines) and then display it in a jsp page. 理想情况下,我想拖尾内容(例如2000行),然后在jsp页面中显示它。 I could use an AJAX call, say ever 100 ms and refresh the data in the UI. 我可以使用AJAX调用,比如说100毫秒,然后刷新UI中的数据。

You can keep the file stream open and keep reading from the position where you are at, either with non-blocking IO (so you don't block if there is no data) or you can check if the file has been modified before you read() using blocking IO. 您可以保持文件流打开,并通过无阻塞IO保持从当前位置读取数据(这样就不会阻塞没有数据的数据),也可以在读取文件之前检查文件是否已被修改()使用阻塞IO。

If you don't want to keep the stream open, you can easily reopen it and skip() to the correct position. 如果不想让流保持打开状态,则可以轻松地将其重新打开并将skip()移到正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM