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...
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
no tail/ajax but there is this
jsp file browser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With