How to display data (.log) in real time in an HTML page?

0

So, I'm trying to make a logger that can be viewed on an HTML page, in real time. The page loads all .log's files (from: / clearing-dit / logs /) and makes it possible to click on each one to show its contents.

<button type="submit" class="list-group-item link">
   <i class="fa fa-fw fa-history"></i> [[${l.filename}]]
   <span class="badge" th:text="${#dates.format(l.lastModified,'dd/MM/YYYY HH:mm:ss')}">10/09/1992 23:55:23</span>
   <span class="badge" th:text="${#numbers.formatDecimal(l.size, 1, 3)}+MB">10MB</span>
 </button>

Once you have selected the file, you will get the last 50 lines of .log (which is like a .txt)

 public void fileNumberLines(Log log) throws IOException {

 InputStream inputStream = new BufferedInputStream(new FileInputStream(logsDir + "/" + log.getFilename()));
    try {
        byte[] byteArray = new byte[1024];
        int count = 0;
        int readChar = 0;
        while ((readChar = inputStream.read(byteArray)) != -1) {
            for (int i = 0; i < readChar; ++i) {
                if (byteArray[i] == '\n') {
                    ++count;
                }
            }
        }
        log.setFinalLine(count);
        log.setInitLine(count - 50 > 0 ? count - 50 : 0);
    } finally {
        inputStream.close();
    }
}

And returned the contents of those 50:

    private String getLogContentByRange(int lineOccurrence, int firstLine, int lastLine, String logPath) {
        BufferedReader buffRead;
        int lineNumber = 1;
        String content = "";
        File file = new File(logPath);

        try {
            buffRead = new BufferedReader(new FileReader(file));
            String line = null;
            do {
                line = "";
                line = buffRead.readLine();
                if (lineNumber <= lastLine && lineNumber >= firstLine) {
                    if (lineNumber == lineOccurrence) {
                        content += "<mark style='background-color: red; color: white;'>" + lineNumber + ". " + line + "</mark>\n";
                    } else {
                        content += lineNumber + ". " + line + "\n";
                    }
                }
                lineNumber++;
            } while (line != null);
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return content;
    }
}

This log is returned and accessed in HTML:

                <div class="panel-body">

                    <pre>
                        <p th:utext="${log.content}">Log content</p>
                    </pre>
                </div>  

My problem is: How do I continue to "print" the information in a file as it is incremented / edited? Is there any way to leave the "idle" check, waiting for new updates? I wanted to do as if the data were being viewed on a console, where it is possible to give the command grep and derivatives.

    
asked by anonymous 16.07.2018 / 20:40

1 answer

1

Speak David! You can make a javascript call that "stands out" from x in x seconds! The problem is that the latency to open a txt file, go through the lines and close this file, and finally render the content on the screen is very high! So, it would be nice of you to put a process in ajax to call a method in the control layer (JSF, Struts or Spring MVC) every 10 seconds.

See this example in Primefaces: link

    
16.07.2018 / 20:54