jquery - Output log file through Ajax in Django -
i have followed so's accepted answer on how read log file in django /var/log/gateway here , managed output file on terminal 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 connect device /home/smartsensor1. device may offline. *
the next step is, want output log file , display in html, did slight modification original code this.
def logs(request): open('../../../../../var/log/gateway') f: while true: line = f.readline() if line: print line return httpresponse(line)
so on client side, put ajax this, based on 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, output data ajax kept on displaying first line of log file. in case, 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 occur because everytime ajax went server, server needs , send output first line of log file , output through httpresponse , completed cycle , never got chance line because completed already. when query done, same thing again, , again.
so possible solution client ask server 1 time, , server keep output log file line line , send out client. not sure if possible, here asking expert on how possibly achieve result can output log file line line/
well, can use idea this:
using get
parameters can control view's method this:
def logs(request): whole_file = true if request.get.get('whole_file') == 0: whole_file = false return_string = '' open('../../../../../var/log/gateway') f: while true: line = f.readline() if line: return_string += line + '<br>' # <br> breakline html, way if not whole_file: break # break if need first line return httpresponse(line)
this way, include variable inside parameter tells view whether return whole log file (first time) or first line (next times).
then in ajax have include way insert get
parameters url. use {{url}}
tag i'm not quite sure how i'm sure you'll find in doc, if can't try other approach static string (not pretty) or create own custom tag it.
this example of possible javascript:
$.ajax({ type: "get", url : "/ajax_log/?whole_file=0", //this request whole file success: function (data) { $("#output").append(data); settimeout("doupdate()", 2000); } }); } settimeout("donextupdate()", 2000); function donextupdate(){ $.ajax({ type: "get", url : "/ajax_log/?whole_file=1", //this request first line success: function (data) { $("#output").append(data); settimeout("doupdate()", 2000); } }); } }
that idea accomplish want. hope helps.
Comments
Post a Comment