Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsp to view log file (like "web tail -f")

Tags:

java

ajax

jsp

log4j

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...

like image 482
reini122 Avatar asked Jul 05 '12 13:07

reini122


2 Answers

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:

  • Java web application that can stream the content of an arbitrary file to the browser (live tail)
  • tail -f in a webbrowser
like image 152
Tomasz Nurkiewicz Avatar answered Sep 29 '22 20:09

Tomasz Nurkiewicz


no tail/ajax but there is this

jsp file browser

like image 20
Kalpesh Soni Avatar answered Sep 29 '22 20:09

Kalpesh Soni