How would you implement a jsp site containing a text area which shows a log file on the (tomcat) server and refreshes automatically.
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...
There's a taglib for that: http://www.servletsuite.com/servlets/tailtag.htm
Put the jar in WEB-INF/lib, the tld in WEB-INF/tags, and you can use:
Tailer provided by Jakarta Common IO library might be helpful. Tailer can act as producer and GUI polling can be consumer.
http://alvinalexander.com/java/jwarehouse/commons-io-2.0/src/test/java/org/apache/commons/io/input/TailerTest.java.shtml
Very good solutions that I didn't know are mention in the thread, here is another one that I found in google- stail
no tail/ajax but there is this
jsp file browser
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:
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 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!
Finally consider using ready-made tools like psi-probe providing this functionality out-of-the-box:
psi-probe http://psi-probe.googlecode.com/svn/wiki/Features/log-tail.png
See also: