如何将日志文件持久地上传到服务器(使用Flask),这些文件会不时更改。

huangapple go评论79阅读模式
英文:

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

问题

假设我在根目录中有一个名为 logs.log 的日志文件,我想在每次附加新日志时读取该文件并使用 Python Flask 将其发送到服务器。

app.py 中,我有以下代码:

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

index.js 中,我有以下代码:

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

每当 logs.log 的内容发生更改时,我希望 logs() 函数更新其内容并使用 receiveLog(); 函数来显示内容。

(也许我整个方法都不正确,如果是这样,请纠正我。)

英文:

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

in app.py
I have code like this:

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

In index.js
I have code like this:

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

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)

答案1

得分: 0

要能查看最新日志,您需要先解决这个问题:

"我如何知道是否有新日志?"

有三种解决方法,正确的解决方案取决于您的产品需求或上市时间:

  1. 轮询

这是最简单的方式,也是最符合您当前解决方案的方式。客户端可以通过重复调用API来代替"知道是否有新日志"。只要客户不太多,且间隔设置为适当的时间(例如5秒?),这是最直接的解决方案。

通常,您可以使用setInterval轻松实现它。

  1. 长轮询

不同于轮询,长轮询是反复发送短暂的请求。长轮询基本上是向服务器发送较少的请求,但每个请求具有较长的超时时间。这给服务器提供了更多的机会返回新的日志。

  1. Web Socket

这是最先进的技术。您应考虑成熟的库,如ws

通过Web Socket,您可以在客户端和服务器之间创建通信通道,因此服务器能够主动告诉客户端"嘿,有新日志"。

您可以决定是否通过Web Socket同时发送新日志,或者在收到服务器的提醒后让客户端发出请求。

英文:

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

huangapple
  • 本文由 发表于 2023年3月12日 17:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定