-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogviewer.py
78 lines (61 loc) · 2.4 KB
/
logviewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
from datetime import datetime
import tornado.ioloop
import tornado.web
from sockjs.tornado import SockJSConnection, SockJSRouter, proto
from config import LOG_DIR
class IndexHandler(tornado.web.RequestHandler):
def get(self):
log_path = LOG_DIR
if log_path is None:
log_path = os.path.realpath(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'logs'))
channels = os.listdir(log_path)
self.render('static/index.html', channels=channels)
class LogViewerHandler(SockJSConnection):
def on_message(self, message):
message = proto.json_decode(message)
if 'channel' in message:
self._channel = message['channel']
self._start_view_log()
self.loop = tornado.ioloop.PeriodicCallback(self._check_new_message, 1000)
self.loop.start()
def _start_view_log(self):
log_path = LOG_DIR
if log_path is None:
log_path = os.path.realpath(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'logs'))
log_path = os.path.join(log_path, self._channel, datetime.now().strftime('%Y'), datetime.now().strftime('%m'))
self._log = os.path.join(log_path, datetime.now().strftime('%d%m%y.log'))
if not os.path.isfile(self._log):
self.send(proto.json_encode({'error': 'Not found logs'}))
self._last_check = os.stat(self._log)[8]
self._last_seek = 0
self._send_new_messages()
def _send_new_messages(self):
with open(self._log) as f:
f.seek(self._last_seek)
messages = f.readlines()
res = []
for m in messages:
msg = m.split('|')
res.append({
'date': msg[0],
'user': msg[1],
'message': '|'.join(msg[2:])
})
self._last_seek = f.tell()
self.send(proto.json_encode(res))
def _check_new_message(self):
if not os.stat(self._log)[8] == self._last_check:
self._last_check = os.stat(self._log)[8]
self._send_new_messages()
LogRouter = SockJSRouter(LogViewerHandler, '/logs')
def main():
app = tornado.web.Application(
[(r"/", IndexHandler)] +
LogRouter.urls
)
app.listen(8080)
print 'Listening on 0.0.0.0:8080'
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()