-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogServer.py
123 lines (98 loc) · 3.45 KB
/
LogServer.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import sys
import logging
from gevent import socket
from gevent.greenlet import Greenlet
from gevent.hub import getcurrent
from gevent.server import StreamServer
import gevent
__all__ = ['LogServer']
class _Greenlet_stdreplace(Greenlet):
_fileobj = None
def switch(self, *args, **kw):
if self._fileobj is not None:
self.switch_in()
Greenlet.switch(self, *args, **kw)
def switch_in(self):
self.saved = sys.stdin, sys.stderr, sys.stdout
sys.stdin = sys.stdout = sys.stderr = self._fileobj
def switch_out(self):
sys.stdin, sys.stderr, sys.stdout = self.saved
self.saved = None
def run(self):
try:
return Greenlet.run(self)
finally:
# XXX why is this necessary?
self.switch_out()
class _fileobject(socket._fileobject):
def __init__(self, log_server, *args, **kwds):
socket._fileobject.__init__(self, *args, **kwds)
self.log_server = log_server
def write(self, data):
try:
self._sock.sendall(data)
except Exception as e:
self.log_server.do_close(self._sock)
if self._sock:
self._sock.close()
self.close()
print type(e), str(e)
def isatty(self):
return True
def flush(self):
pass
def readline(self, *a):
return socket._fileobject.readline(self, *a).replace("\r\n", "\n")
class LogServer(StreamServer):
def __init__(self, listener, logger, **server_args):
StreamServer.__init__(self, listener, spawn=_Greenlet_stdreplace.spawn, **server_args)
self.logger = logger
def handle(self, socket, address):
f = getcurrent()._fileobj = _fileobject(self, socket)
getcurrent().switch_in()
try:
formatter = logging.Formatter('[%(asctime)s] %(levelname)s - %(message)s')
s_handler = logging.StreamHandler()
s_handler.setFormatter(formatter)
self.logger.addHandler(s_handler)
socket.logger_handler = s_handler
socket.peer_name = str(socket.getpeername())
except Exception as e:
self.logger.info("Get log connection ERROR %s" % str(e))
return
finally:
self.logger.info("Finish accept connection")
self.logger.info("Get log connection from %s SUCCESS" % str(socket.getpeername()))
def serve_forever(self, *args, **kwds):
return gevent.spawn(super(LogServer, self).serve_forever, *args, **kwds)
def do_close(self, socket, *args):
if not socket:
return
self.logger.removeHandler(socket.logger_handler)
socket.close()
self.logger.info("RemoveHandler from peer %s" % socket.peer_name)
if __name__ == '__main__':
import datetime
import logging
import gevent
def InitLog():
' InitLog'
global _normal
now = datetime.datetime.now()
handler = logging.FileHandler("./log/abs_log_{0}.txt".format(now.strftime("%Y%m%d_%H%M%S")))
formatter = logging.Formatter('[%(asctime)s] %(levelname)s - %(message)s')
handler.setFormatter(formatter)
_normal = logging.getLogger('_normal')
_normal.addHandler(handler)
_normal.setLevel(logging.DEBUG)
def Func1():
i = 0
while i < 10:
i += 1
_normal.info(1)
gevent.sleep(1)
work = gevent.spawn(Func1)
InitLog()
server = LogServer(('127.0.0.1', 9000), _normal)
server.serve_forever()
gevent.wait([work])