Display the contents of a log file as it is updated

29,006

Solution 1

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.

Solution 2

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

Share:
29,006

Related videos on Youtube

Sina Sh
Author by

Sina Sh

Updated on July 09, 2022

Comments

  • Sina Sh
    Sina Sh almost 2 years

    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?