Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在Django中通过Ajax输出日志文件_Jquery_Python_Ajax_Django - Fatal编程技术网

Jquery 在Django中通过Ajax输出日志文件

Jquery 在Django中通过Ajax输出日志文件,jquery,python,ajax,django,Jquery,Python,Ajax,Django,我遵循了SO的公认答案,即如何从/var/log/gateway中读取Django中的日志文件,并成功地在终端上输出了该文件,如下所示 2013-05-09T11:15:02.539091+08:00 localhost gateway[5205]: System starting up... 2013-05-09T12:57:44.160246+08:00 localhost gateway[5205]: System start complete. 2013-05-09T15:13:47.4

我遵循了SO的公认答案,即如何从/var/log/gateway中读取Django中的日志文件,并成功地在终端上输出了该文件,如下所示

2013-05-09T11:15:02.539091+08:00 localhost gateway[5205]: System starting up...
2013-05-09T12:57:44.160246+08:00 localhost gateway[5205]: System start complete.
2013-05-09T15:13:47.428553+08:00 localhost gateway[4777]: * Unable to connect to device /home/smartsensor1. Device may be offline. *
下一步是,我想输出日志文件并在html中显示它,我对原始代码做了一些修改,如下所示

def Logs(request):
    with open('../../../../../var/log/gateway') as f:
        while True:
            line = f.readline()
            if line:
                print line
                return HttpResponse(line)
因此,在客户端,我根据另一个So的公认答案,这样放置Ajax

这样,来自Ajax的输出数据将继续显示日志文件的第一行。在这种情况下,是这样的吗

2013-05-09T11:15:02.539091+08:00 localhost gateway[5205]: System starting up...
2013-05-09T11:15:02.539091+08:00 localhost gateway[5205]: System starting up...
2013-05-09T11:15:02.539091+08:00 localhost gateway[5205]: System starting up...
我知道会发生这种情况,因为每次ajax进入服务器时,服务器都会做它需要做的事情,并通过HttpResponse发送回日志文件的第一行输出,并完成循环,但它从来没有机会执行另一行,因为它已经完成了。当另一个查询完成时,它会一次又一次地执行相同的操作


因此,可能的解决方案是客户机向服务器询问一次,服务器保持逐行输出日志文件并将其发送给客户机。我不确定这是否可行,因此我在这里询问任何专家如何可能实现结果,我可以逐行输出日志文件

def Logs(request):
    whole_file = True
    if request.GET.get('whole_file') == 0:
        whole_file = False
    return_string = ''
    with open('../../../../../var/log/gateway') as f:
        while True:
            line = f.readline()
            if line:
                return_string += line + '<br>' # <br> is for breakline html, do it your way
                if not whole_file: break # break if you only need first line
    return HttpResponse(line)
使用
GET
参数,您可以如下控制视图的方法:

def Logs(request):
    whole_file = True
    if request.GET.get('whole_file') == 0:
        whole_file = False
    return_string = ''
    with open('../../../../../var/log/gateway') as f:
        while True:
            line = f.readline()
            if line:
                return_string += line + '<br>' # <br> is for breakline html, do it your way
                if not whole_file: break # break if you only need first line
    return HttpResponse(line)

这就是我的想法,去实现你想要的。我希望它能有所帮助。

我找到了一种解决方法,就是执行服务器发送事件

在服务器端,我使用依赖于

如果logtime>=currenttime:我在上面做的检查是确保html只在加载页面后打印日志,而不是整个日志文件

然后在HTML5方面,我做了与我提供的第一个链接中作者所做的示例类似的事情

<div id="tab9" class="tab_content">
    <table>
        <tr>
            <td>
                <p> Shows the records of the logs on gateway.</p>
                <div style="border:1px solid; height: 200px; width: 850px; overflow: auto;" id="output"></div>
            </td>
        </tr>
    </table>
</div>
<script>
$(document).ready(function() {
    var source = new EventSource('/gtwy/logging/');
    var events_dom = $("#output");

    source.addEventListener("line", function(e) {
        events_dom.append(e.data + "<br>");
    });
});
</script>

显示网关上的日志记录

$(文档).ready(函数(){ var source=neweventsource('/gtwy/logging/'); var events_dom=$(“#输出”); source.addEventListener(“行”,函数(e){ 事件\u dom.append(e.data+“
”); }); });

使用这个,我成功地显示了可用的日志。我面临的唯一问题是创建网关文件本身,它需要权限,因为我正试图在
/var/log

中创建它,保持连接打开并在生成时发送附加数据称为长轮询,但我不确定如何在django中实现这一点。另一方面,考虑使用绝对的,而不是相对URL -当你的应用程序安装在子目录中时会发生什么?你可以使用WebSokSoSook建立持久的连接。让我看看有关日志轮询的内容,也许它会给我一些关于如何解决我的问题的提示。嗯,谢谢你对url的评论,但是当我使用绝对url而不是相对url时,会不会造成同样的问题?我是说它还是找不到?@dm03514谢谢。我忘了在我的问题中提到,应用任何并行服务器目前都不是一个选项。因此,我必须使用ajax,或者暂时研究一下Basic提到的长轮询。re:url
/var/log/blah
将始终指向同一个位置<代码>。/../../var/log/blah将根据请求路径的位置更改其指向的位置。例如,从
/srv/www/test
它将指向
/var/log/blah
,但从
/srv/www/test/something
它将指向
/srv/var/log/blah
import os
from django.conf import settings
from django.views.generic import View
from django_sse.views import BaseSseView
import time
from django.utils.timezone import now
from datetime import datetime
from django.utils.timezone import now
from dateutil import parser

class MySseEvents(BaseSseView):
    def iterator(self):
        while True:
            if not os.path.exists('/var/log/gateway'):
                file('/var/log/gateway', 'w+').close()
            with open('/var/log/gateway') as f:
                while True:
                    #get the current time
                    currenttime = now()
                    #read a line from gateway
                    line = f.readline()
                    if line:
                        #split the line read into 4 section
                        str = line.split(' ',3)
                        #compare if the time from the log file is less than the current time
                        logtime = parser.parse(str[0])
                        if logtime >= currenttime:
                            #print when the log is written after the current time
                            output = '%s: %s' % (datetime.strftime(logtime,'%d/%m %H:%M %p'), str[3])
                            self.sse.add_message("line", output)
                            time.sleep(1)
                            yield
<div id="tab9" class="tab_content">
    <table>
        <tr>
            <td>
                <p> Shows the records of the logs on gateway.</p>
                <div style="border:1px solid; height: 200px; width: 850px; overflow: auto;" id="output"></div>
            </td>
        </tr>
    </table>
</div>
<script>
$(document).ready(function() {
    var source = new EventSource('/gtwy/logging/');
    var events_dom = $("#output");

    source.addEventListener("line", function(e) {
        events_dom.append(e.data + "<br>");
    });
});
</script>