Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the contents of a log file as it is updated

Tags:

I have external programs such as ffmpeg and gstreamer running in the background and writing to a log file. I want to display the contents of this log with my Flask application, so that the user can watch the log update, like tail -f job.log would do in the terminal.

I tried to use <object data="/out.log" type="text/plain"> to point at the log file, but that failed to show the data, or the browser told me I needed a plugin.

How can I embed and update the log file in an HTML page?

like image 401
Sina Sh Avatar asked Feb 21 '16 19:02

Sina Sh


People also ask

What command can you use to view contents of a log file?

Linux logs will display with the command cd/var/log. Then, you can type ls to see the logs stored under this directory. One of the most important logs to view is the syslog, which logs everything but auth-related messages.

What is the command used to see continuously updating log file?

tail Command – Monitor Logs in Real Time As said, tail command is the most common solution to display a log file in real time. However, the command to display the file has two versions, as illustrated in the below examples. In the first example the command tail needs the -f argument to follow the content of a file.

How do I view a log file?

You can read a LOG file with any text editor, like Windows Notepad. You might be able to open one in your web browser, too. Just drag it directly into the browser window, or use the Ctrl+O keyboard shortcut to open a dialog box to browse for the file.

How do I view a log file in Linux?

This is such a crucial folder on your Linux systems. Open up a terminal window and issue the command cd /var/log. Now issue the command ls and you will see the logs housed within this directory (Figure 1).


2 Answers

Use a Flask view to continuously read from the file forever and stream the response. Use JavaScript to read from the stream and update the page. This example sends the entire file, you may want to truncate that at some point to save bandwidth and memory. This example sleeps between reads to reduce cpu load from the endless loop and allow other threads more active time.

from time import sleep from flask import Flask, render_template  app = Flask(__name__)  @app.route('/') def index():     return render_template('index.html')  @app.route('/stream') def stream():     def generate():         with open('job.log') as f:             while True:                 yield f.read()                 sleep(1)      return app.response_class(generate(), mimetype='text/plain')  app.run() 
<pre id="output"></pre> <script>     var output = document.getElementById('output');      var xhr = new XMLHttpRequest();     xhr.open('GET', '{{ url_for('stream') }}');     xhr.send();      setInterval(function() {         output.textContent = xhr.responseText;     }, 1000); </script> 

This is almost the same as this answer, which describes how to stream and parse messages, although reading from an external file forever was novel enough to be it's own answer. The code here is simpler because we don't care about parsing messages or ending the stream, just tailing the file forever.

like image 143
davidism Avatar answered Sep 20 '22 02:09

davidism


I am using frontail package from npm.

npm i frontail -g frontail /var/log/syslog 

visit http://127.0.0.1:9001 to view logs

Source: https://github.com/mthenw/frontail

This may not be the exact answer for the question(to embed an html page), but it solves the problem of many users who are looking specifically only for

Display the contents of a log file as it is updated

like image 42
Ayushya Avatar answered Sep 22 '22 02:09

Ayushya