简体   繁体   English

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

[英]jsp to view log file (like “web tail -f”)

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

I've no idea how to detect the changed lines in the log... 我不知道如何检测日志中更改的行...

and polling the server every few seconds is a good idea, but using / / will be much more effective and you won't experience any latency. 并每隔几秒轮询一次服务器是一个好主意,但使用 / / 会更有效,你不会遇到任何延迟。

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... 使用AJAX调用的时间戳参数说: 给我后面的所有日志行...

    This solution is very ineffective and will generate a lot of I/O traffic 此解决方案效率很低,会产生大量I / O流量

  • 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 appender and keep most recent logs in memory. 编写自定义 appender并将最新日志保存在内存中。 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-probe providing this functionality out-of-the-box: 最后考虑使用现成的工具,如psi-probe,提供开箱即用的功能:

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

See also: 也可以看看:

no tail/ajax but there is this 没有尾巴/ ajax但是有这个

jsp file browser jsp文件浏览器

There's a taglib for that: http://www.servletsuite.com/servlets/tailtag.htm 有一个taglib: 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中,WEB-INF / tags中的tld,你可以使用:

<%@ 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>

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

Tailer provided by Jakarta Common IO library might be helpful. Jakarta Common IO库提供的Tailer可能会有所帮助。 Tailer can act as producer and GUI polling can be consumer. 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

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

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