I followed the accepted SO answer on how to read the log file in Django from / var / log / gateway from here , and I was able to output the file to the 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. *
Next step: I want to output the log file and display it in html, I did this with a little modification of the source 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 answer we accepted here .
$.ajax({ type: "GET", url : "{% url WebServiceApp.logging.Logs %}", success: function (data) { $("#output").append(data); setTimeout("doUpdate()", 2000); } }); } setTimeout("doUpdate()", 2000);
The Ajax output is saved when the first line of the log file is displayed. Where in this case, 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 happens because every time ajax goes to the server, the server does what it needs and sends back the output, which is the first line of the log file and output via HttpResponse, and it completes the loop, and it does did not get the opportunity to make another line, because it is already completed. When another request is executed, it does the same thing again and again.
Thus, a possible solution is a client that requests the server once, and the server continues to display the log file line by line and send it to the client. I'm not sure if this is possible, so I ask any expert on how to possibly achieve a result, where I can output the log file by line /