JSP来查看日志文件(如"网络尾-f") [英] jsp to view log file (like "web tail -f")

查看:131
本文介绍了JSP来查看日志文件(如"网络尾-f")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你将如何实现一个jsp网站上包含一个文本区域,其中显示了(Tomcat)的服务器上的日志文件,并自动刷新。

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

我觉得刷新是简单的使用setTimeout和发送Ajax请求轮询到服务器。但问题是如何监视服务器上的文件(这是一个Log4J的日志文件 - 也许我可以使用自己的附加目的地)?更改只发送更改的行当AJAX请求到达

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?

我不知道如何检测变线在日志中...

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

推荐答案

AJAX 并轮询服务器每隔几秒钟是一个好主意,但使用的彗星 /的服务器推送 / 会要有效得多,你会不会遇到任何延迟。

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

至于服务器端,你有几种选择:

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

  • 每一个用户请求新数据时打开文件,去年底发出最后几行。你需要以某种方式表明了哪个行数据发送的最后一次避免发送同样的台词多次或缺少其中的一部分。使用时间戳参数AJAX调用说:后给我所有的日志行...

这个解决方案是非常无效的,会产生大量的I / O流量

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 log4j appender 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!

最后考虑使用像PSI-探头现成的工具提供此功能即开即装即用的:

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

  • Java web application that can stream the content of an arbitrary file to the browser (live tail)
  • tail -f in a webbrowser

这篇关于JSP来查看日志文件(如"网络尾-f")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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