Jsp to view the log file (for example, "web tail -f")

How would you implement a jsp site containing a text area that displays a log file on the server (tomcat) and is automatically updated.

I think the update is just for polling on the server using setTimeout and sending an ajax request. But the problem is how to control the file on the server (is it a Log4J Logfile - maybe I can use my own appender?) To change and send only the changed lines when the ajax request arrives?

I do not know how to detect changed lines in the log ...

+6
source share
5 answers

and polling the server every few seconds is a good idea, but using / / will be much more efficient and you will not experience latency.

As for the server side, you have several options:

  • open the file every time the user requests new data, go to the end and send the last lines. You need to somehow indicate to which lines the data was last sent, so as not to send the same lines several times or to skip some of them. Use the timestamp argument to invoke AJAX to say: give me all the lines of the log after ...

    This solution is very inefficient and will generate a lot of I / O traffic.

  • Keep an open stream in the log file for each client and when the client requests new lines, read as much as possible (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 the latest logs in memory. When clients ask, just dump the contents of this buffer (the same time stamp restrictions apply)

    Very reliable, but watch out for memory usage.

  • Finally, consider using pre-built tools like psi-probe , which provide this functionality out of the box:

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

See also:

+10
source

no tail / ajax, but there is this

jsp file browser

+2
source

There is a taglib for this: http://www.servletsuite.com/servlets/tailtag.htm

Put the jar in WEB-INF / lib, tld in the WEB-INF / tags, and you can use:

<%@ taglib uri="taglib.tld" prefix="t" %> <!-- read last 50 rows and print them --> <t:tail file="c:/webserver/log.txt" count="50" id="S"> <br><%=S%> </t:tail> 
+1
source

Very good solutions that I did not know are mentioned in the stream, here is another one that I found in google- stail

0
source

The Tailer provided by the Jakarta Common IO library may be useful. Tailer can act as a producer and survey GUI.

http://alvinalexander.com/java/jwarehouse/commons-io-2.0/src/test/java/org/apache/commons/io/input/TailerTest.java.shtml

0
source

Source: https://habr.com/ru/post/919758/


All Articles