forked from mikeplavsky/httplog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
86 lines (66 loc) · 2.25 KB
/
server.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
import cherrypy
from cherrypy import expose
from Queue import Queue, Empty
import sys
class Root:
@expose
def log(self,*args,**kwargs):
rec = kwargs[ 'record' ]
import pickle
record = pickle.loads( rec )
cherrypy.engine.log_cache.put(record)
return 'Done'
import ini
import os
svc_path = os.path.join(os.path.dirname(__file__), 'httpservice.conf')
def getIntConfigValue(name):
return int(getConfigValue(name))
def getConfigValue(name):
return ini.read( svc_path, 'default',name)
def Register():
from logging.handlers import RotatingFileHandler
from logging import Formatter
h = {}
formatter = Formatter("%(asctime)s [%(process)s:%(thread)s] ** %(levelname)s ** %(message)s")
logsnames = getConfigValue('lognames')
qsize = getIntConfigValue('qsize')
rotation_bytes = getIntConfigValue('rotation_bytes')
rotation_count = getIntConfigValue('rotation_count')
for i in logsnames.split(","):
h[i] = RotatingFileHandler(os.path.join(os.path.dirname(__file__), i), maxBytes=rotation_bytes, backupCount=rotation_count)
h[i].formatter = formatter
def write():
while True:
try:
rec = cherrypy.engine.log_cache.get_nowait()
h[rec.name].emit(rec)
except Empty:
return
from cherrypy.process import plugins
pq = plugins.Monitor(cherrypy.engine,write,3)
pq.subscribe()
cherrypy.engine.log_cache = Queue(maxsize = qsize)
conf_path = os.path.join(os.path.dirname(__file__), "config.txt")
cherrypy.config.update(conf_path)
app = cherrypy.quickstart( Root() )
from ServiceBase import ServiceLauncher
class HTTPLogService(ServiceLauncher):
_svc_name_ = _svc_display_name_ = ini.read( svc_path, 'default','name' )
_svc_description_ = ini.read( svc_path, 'default', 'description' )
def __init__(self, args):
ServiceLauncher.__init__(self, args, Register)
from ServiceBase import ServiceHelperBase
class HTTPLogServiceHelper(ServiceHelperBase):
Class = HTTPLogService
File = __file__
def InstallAsService():
service = HTTPLogServiceHelper()
service.Stop()
service.Remove()
service.Install()
service.Start()
if __name__ == '__main__':
if sys.argv.__len__() > 1 and sys.argv[1] == "installservice":
InstallAsService()
else:
Register()