Java – JSP view log file (e.g. “web tail – F”)

How to implement a JSP site containing a text area that displays the log files on the (Tomcat) server and refreshes automatically

I think refresh is easy to use setTimeout to poll the server and send Ajax requests But the problem is how to monitor the file on the server (it's a log4j log file - maybe I can use my own appender?) Make changes and send only changed rows when an Ajax request arrives?

I don't know how to detect changed lines in the log

Solution

Ajax and polling the server every few seconds is a good idea, but using comet / server push / websocket is more effective and you won't encounter any delays

On the server side, you have several choices:

>Each time the user requests new data, open the file, go to the last and send the last line You need to indicate the last row data sent in some way to avoid sending the same row multiple times or losing some of them Using the timestamp parameter of Ajax call, say: give me all the subsequent log lines

This solution is inefficient and will generate a lot of I / O traffic > save the open stream to the log file of each client. When the client requests a new line, read as much as possible (of course, it will not block)

Better, but not well extended (too many open files, I'm here) > write a custom log4j appender and save the latest log in memory When the client asks, it only needs to dump the contents of this buffer (the same restrictions apply to timestamp)

Very powerful, but pay attention to memory usage! > Finally, consider using the ready-made tools like psi probe to provide out of the box functions:

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

You can also see:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>