How to upload log file persistently to server (using flask) which is changed every now and then


Suppose I have a log file named logs.log in root folder,
I want to read that file whenever a new log is appended and send it to server using python flask

I have code like this:

@app.route("/logs", methods=['GET'])
def logs():
    if 'api_key' in session:
            file = open("logs.log", "r")
        except Exception as e:
            raise "Error %s" % e

In index.js
I have code like this:

function receiveLog() {
  var xhr = new XMLHttpRequest();
  url = '/logs''GET', url, true)
  xhr.setRequestHeader("Content-Type", "multipart/form-data");
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      text = xhr.responseText;

Whenever logs.log content is changed, i want logs() function to update its content and use receiveLog(); function to display the content.

(perhaps i am doing the whole thing wrong if so then please correct me)

Asked By: Oxy Op



To be able to view the latest logs, you need to solve this problem first

"How do I know there are new logs?"

There are 3 ways to solve it and the right solution depends on your product requirements or time to market

  1. Polling

This is the most easiest way and the one best matches your current solution. Instead of "knowing there are new logs", the client can just make API call repeatedly. As long as there are not so many clients and interval is set to a proper number (e.g. 5 secs?). This is the most straightforward solution.

Usually, you can use setInterval to achieve it easily

  1. Long polling

Unlike polling is making short-lived request repeatedly. Long polling is basically sending less requests to server but each request has a longer timeout duration. This gives sever bigger opportunity to have new logs to return.

  1. Web Socket

This is the most advanced technique. You should consider mature library such as ws

With web socket, you create communication channel between client and server so your server is able to proactively tell the client "Hey, there are new logs".

You can decide whether to send new logs via web socket at the same time or let client make a request once receiving a heads-up from the server

Answered By: Even