动态输出日志

Posted 3one

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态输出日志相关的知识,希望对你有一定的参考价值。

后端:

def test(request):

import os
import time
t = time.time()
logfilename = time.strftime(‘%Y%m%d%H%M%S‘) + ‘.log‘
# print logfilename
# status, out = os.system("/tmp/sss.sh + ‘ ‘ + ‘test‘>/tmp/%s" % logfilename)
# out = os.system("/tmp/sss.sh ‘test‘>/tmp/%s" % logfilename)
logfilepath = ‘/tmp/%s‘ % logfilename
t = threading.Thread(target=shell_os, args=(logfilename,))
t.setDaemon(True)
t.start()
f = file(‘/tmp/20160903083315.log‘, ‘r+‘)
# time.sleep(10)
# out = f.readline()
# pos = f.tell()
# print out
# f.close()
# return HttpResponse(out)
return render_to_response(‘myajax.html‘, {‘title‘: logfilepath, ‘output‘: ‘test‘,})


import os


def shell_os(logfilename):
out = os.system("/tmp/sss.sh ‘test‘>/tmp/%s" % logfilename)
return out


def myajax(request):
# print request.method

if request.method == ‘POST‘:
print request.method
print request
filepath = request.POST.get(‘filepath‘)
print filepath
pos = request.POST.get(‘pos‘)
print pos

f = file(filepath, ‘r+‘)
print ‘-------------‘
f.seek(int(pos))
print ‘-------------‘
out = f.readline()
pos = f.tell()
f.close()
print ‘==========out‘, out, pos
ret = {‘out‘: out, ‘posh‘: pos}
return HttpResponse(json.dumps(ret))
else:
return HttpResponse(‘GET‘)

前端:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<style>
.top {
margin: 20px;
padding: 20px;
border: 1px solid #ddd;
}

.commom {
margin: 20px;
padding: 20px;
border: 1px solid #ddd;
}

</style>
</head>
<body>
<div class="top">
<div style="float: left">[正在执行]</div>
<div style="float: left">{{ title }}</div>
</div>
<div class="commom">{{ output }}</div>

</body>
<script src="/statics/jquery/jquery-3.1.0.min.js" type=text/javascript></script>
<script type=text/javascript>
pos = 0;
function myajax() {
//console.log({{ title }})
var filepath = "{{ title }}";

//console.log(filepath);
$.ajax({
url: "/web/myajax/",
type: "POST",
data: {filepath: filepath, pos: pos},
success: function (callback) {
var obj = jQuery.parseJSON(callback);
pos = obj.posh;
var old_html = $(".commom").html();
var new_html = old_html + obj.out + "<br>";
if (obj.out.indexOf(‘FINISH‘) > 0) {
clearInterval(logAjax)
}
if (obj.out == ‘‘) {
console.log(obj.out);
}
else {
$(".commom").html(new_html);

}

//console.log(obj.out);
//console.log($(".commom").html())
},
error: function () {
console.log(‘error‘)
}

})
}

var logAjax = setInterval(‘myajax()‘, 500);
</script>
</html>

 

#用clearInterval(logAjax )来结束

 

以上是关于动态输出日志的主要内容,如果未能解决你的问题,请参考以下文章

python分析apache和nginx日志文件输出访客ip列表的代码

JAVA之AOP

argparse 代码片段只打印部分日志

常用python日期日志获取内容循环的代码片段

动态 Rstudio 代码片段

mybatis学习日志二