java jsp 查看日志文件(如“web tail -f”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11345387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:43:21  来源:igfitidea点击:

jsp to view log file (like "web tail -f")

javaajaxjsplog4j

提问by reini122

How would you implement a jsp site containing a text area which shows a log file on the (tomcat) server and refreshes automatically.

您将如何实现包含文本区域的 jsp 站点,该文本区域显示(tomcat)服务器上的日志文件并自动刷新。

I think the refresh is simple to poll to the server using setTimeout and sending an ajax request. But the problem is how to monitor the file on the server (it is a Log4J Logfile - maybe I can use an own appender?) for changes and sending only the changed lines when the ajax request arrives?

我认为刷新很简单,可以使用 setTimeout 轮询服务器并发送 ajax 请求。但问题是如何监视服务器上的文件(它是一个 Log4J 日志文件 - 也许我可以使用自己的 appender?)进行更改并在 ajax 请求到达时仅发送更改的行?

I've no idea how to detect the changed lines in the log...

我不知道如何检测日志中更改的行...

回答by Tomasz Nurkiewicz

ajaxand polling the server every few seconds is a good idea, but using comet/server-push/websocketwill be much more effective and you won't experience any latency.

ajax并每隔几秒轮询一次服务器是个好主意,但使用Comet/ server-push/ websocket会更有效,而且您不会遇到任何延迟。

With regards to server-side, you have few options:

关于服务器端,您有几个选择:

  • open the file every time the user requests new data, go to the end and send last lines. You need to somehow indicate up to which line data was sent the last time to avoid sending the same lines multiple times or missing some of them. Use a timestamp argument to AJAX call to say: give me all log lines after...

    This solution is very ineffective and will generate a lot of I/O traffic

  • Keep open stream to log file for each client and when client asks for new lines, read as much as you can (of course without blocking).

    Much better, but won't scale well (too many open files, here I come)

  • Write a custom log4jappender and keep most recent logs in memory. When clients asks, just dump the contents of this buffer (same restrictions on timestamp apply)

    Very robust, but watch out for memory usage!

  • Finally consider using ready-made tools like psi-probeproviding this functionality out-of-the-box:

    psi-probe http://psi-probe.googlecode.com/svn/wiki/Features/log-tail.png

  • 每次用户请求新数据时打开文件,转到最后并发送最后一行。您需要以某种方式指示上次发送的行数据,以避免多次发送相同的行或丢失其中的一些行。使用 AJAX 调用的时间戳参数说:在...之后给我所有日志行

    这种方案效率很低,会产生大量的I/O流量

  • 保持打开流以记录每个客户端的日志文件,当客户端要求新行时,尽可能多地阅读(当然不要阻塞)。

    好多了,但不能很好地扩展(打开的文件太多,我来了)

  • 编写自定义log4jappender 并将最近的日志保存在内存中。当客户端询问时,只需转储此缓冲区的内容(对时间戳的限制相同)

    非常健壮,但要注意内存使用!

  • 最后考虑使用现成的工具,如 psi-probe,提供开箱即用的功能:

    psi 探针 http://psi-probe.googlecode.com/svn/wiki/Features/log-tail.png

See also:

也可以看看:

回答by Kalpesh Soni

no tail/ajax but there is this

没有尾巴/阿贾克斯,但有这个

jsp file browser

jsp文件浏览器

回答by GreyFairer

There's a taglib for that: http://www.servletsuite.com/servlets/tailtag.htm

有一个标签库:http: //www.servletsuite.com/servlets/tailtag.htm

Put the jar in WEB-INF/lib, the tld in WEB-INF/tags, and you can use:

把jar放在WEB-INF/lib下,tld放在WEB-INF/tags下,就可以使用:

<%@ taglib uri="taglib.tld" prefix="t" %> 

<!-- read last 50 rows and print them --> 
<t:tail file="c:/webserver/log.txt" count="50" id="S"> 
  <br><%=S%> 
</t:tail>

回答by shem

Very good solutions that I didn't know are mention in the thread, here is another one that I found in google- stail

线程中提到了我不知道的非常好的解决方案,这是我在google-stail 中找到的另一个解决方案

回答by Deep

Tailer provided by Jakarta Common IO library might be helpful. Tailer can act as producer and GUI polling can be consumer.

Jakarta Common IO library 提供的 Tailer 可能会有所帮助。Tailer 可以充当生产者,GUI 轮询可以充当消费者。

http://alvinalexander.com/java/jwarehouse/commons-io-2.0/src/test/java/org/apache/commons/io/input/TailerTest.java.shtml

http://alvinalexander.com/java/jwarehouse/commons-io-2.0/src/test/java/org/apache/commons/io/input/TailerTest.java.shtml