Reputation: 946
I am trying to create a logger window for my website that streams python logger messages to a javascript popup window. I've gotten to the point where if I close the window and reopen it new messages will be displayed, but I want to write some javascript that automatically refreshes the window (and calls dajaxice) every N seconds.
My ajax.py:
@dajaxice_register(method='GET')
def getLogs(request):
fname = "/path/to/LOG_2015-07-08.log"
with open(fname,"r") as f:
lines = f.readlines()
lines = lines[-15:] //
logger.info("Displaying Logs")
return json.dumps({'message':lines})
My html:
<script language="javascript" type="text/javascript">
function popitup(data) {
sessionStorage.setItem("logs",data.message);
newWindow = window.open('/InterfaceApp/LogViewer', 'Log Viewer', 'height=400,width=800,status=yes');
if(newWindow && !newWindow.closed){
newWindow.location.reload(true); //this is my first attempt at a refresh, wasn't quite what I wanted.
newWindow.focus();
}
}
</script>
<div class="container">
<input id="LogMessages" type="button" value="View Log Messages" onclick="Dajaxice.InterfaceApp.getLogs(popitup)"/>
</div>
To reiterate, I want to click the button and have a popup window come up. I want that popup window to refresh every N seconds with the last 15 lines of my log file (the lines are added to the log every time the user navigates around the website). The dajaxice function in ajax.py
is what grabs the log files so that somehow call somehow needs to be included in the refresh.
Can anyone help me out with this? I've been struggling with it for days.
Thanks!!
Upvotes: 0
Views: 41
Reputation: 946
I was able to figure it out on my own. Code below worked for me.
ajax.py:
@dajaxice_register(method='GET')
def getLogs(request):
fname = "/path/to/LOG_2015-07-09.log"
with open(fname,"r") as f:
lines = f.readlines()
lines = lines[-15:]
return json.dumps({'message':lines})
html:
<div class="container-fluid">
<h4 class="text-center">Log Messages</h4>
<div class="content">
<span class='value'></span>
</div>
<script language="javascript" type="text/javascript">
function saveLogs(data){
sessionStorage.setItem("logs",data.message);
}
$(document).ready(
function() {
setInterval(function() {
Dajaxice.InterfaceApp.getLogs(saveLogs);
var logs = sessionStorage.getItem("logs");
document.querySelector('.content .value').innerText = logs;
}, 3000);
});
</script>
</div>
I used dajaxice instead of straight ajax, which took quite a bit of fiddling with before all of the settings were configured correctly, but it seemed easier than trying to learn how to integrate ajax and php into my django project.
Upvotes: 0