简体   繁体   English

在网页上显示过程日志

[英]Display process log on a web page

I've got a web page that allows to start a certain process and then redirects to another page that displays log file of that process. 我有一个网页,该网页允许启动某个进程,然后重定向到另一个页面,该页面显示该进程的日志文件。 Since execution takes up to 10 minutes, I want log page to autoupdate itself or load data from the file periodically. 由于执行过程最多需要10分钟,因此我希望日志页面自动更新自身或定期从文件中加载数据。

Right now I added 现在我加了

<meta http-equiv="refresh" content="5;url=log.php#bottom" /> 

to html/head but wondering if there may be a better solution. 到html / head,但想知道是否有更好的解决方案。 Can someone give any advice on aproaching this problem? 有人可以提供有关解决此问题的任何建议吗?

You could: 你可以:

  • Periodically poll the server to see if there are more messages, basically you would call a PHP script with javascript and would pass the length of the log file in the last poll and then insert into the document the new data. 定期轮询服务器以查看是否还有更多消息,基本上,您将使用javascript调用PHP脚本,并在上次轮询中传递日志文件的长度,然后将新数据插入文档中。 The server would return all the data after that offset and also the new length. 服务器将返回该偏移量之后的所有数据以及新的长度。
  • (simpler) Make a long lived PHP script that keeps reading the file and echo and flush it as soon as there's new data. (更简单)制作一个使用寿命长的PHP脚本,该脚本会不断读取文件,并在有新数据时立即回显并刷新它。 See PHP: How to read a file live that is constantly being written to . 请参阅PHP:如何实时读取不断写入的文件

I do it this way: 我是这样做的:

var current_length = 0;
function update() {
  setTimeout(update, 3000);
  $.post("/update_url", { 'current_length': current_length }, function(data) {
    if (data.current_length != current_length) return; //it's too old answer
    $("#log").html($("#log").html() + data.text);
    current_length += data.text.length;
  }, "json"); 
}
update();

The server must skip several bytes at beginning and send json with current_length and the rest of file. 服务器必须在开始时跳过几个字节,并使用current_length和文件的其余部分发送json。

I prefer using memcached to store process output. 我更喜欢使用memcached来存储进程输出。

Use AJAX to do this. 使用AJAX执行此操作。 Easy in jQuery: 在jQuery中简单:

    <script type="text/javascript">

    $(function(){
        window.setInterval('updateLog()', 5000);
    });

    function updateLog() {
        $.get('log.php');
    }

    </script>

Why not use javascript? 为什么不使用JavaScript?

Use setInterval and run an AJAX call to log.php periodically. 使用setInterval并定期运行AJAX调用log.php。

You could also use an iframe, but the AJAX perdiodical call is a better way of doing it in my opinion. 您也可以使用iframe,但我认为AJAX定期调用是一种更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM