I have followed an SO's accepted answer on how to read the log file in Django from /var/log/gateway from here and I managed to output the file on terminal like here.
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. *
The next step is, I want to output the log file and display it in html, I did it with slight modification from the original code like this.
def Logs(request):
with open('../../../../../var/log/gateway') as f:
while True:
line = f.readline()
if line:
print line
return HttpResponse(line)
So on the client side, I put Ajax like this, based on another SO's accepted answer here.
$.ajax({
type: "GET",
url : "{% url WebServiceApp.logging.Logs %}",
success: function (data) {
$("#output").append(data);
setTimeout("doUpdate()", 2000);
}
});
}
setTimeout("doUpdate()", 2000);
With this, the output data from Ajax kept on displaying the first line of the log file. Where in this case, is like this
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...
I know this occur because everytime ajax went to the server, the server does what it needs to do and send back the output which is the first line of the log file and output through the HttpResponse and completed the cycle and it never got a chance to do another line because it is completed already. When another query is done, it does the same thing again, and again.
So the possible solution is client ask the server one time, and the server keep output the log file line by line and send it out to client. I am not sure if this is even possible, so here I am asking any expert on how to possibly achieve the result where I can output the log file line by line/