-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue_from_logs.py
52 lines (40 loc) · 1.65 KB
/
queue_from_logs.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
import simplejson
from redis import Redis
from os import listdir
from os.path import isdir, join
from time import sleep
from utils import parseline
import sys
import logging
logger = logging.getLogger("queuefromlogs")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
logpath = "logs"
out_queue = "q:loglines"
if __name__ == "__main__":
if len(sys.argv) >= 2:
out_queue = sys.argv[1]
if len(sys.argv) > 2 and isdir(sys.argv[2]):
logpath = sys.argv[2]
logger.info("Adding log lines from log files in %s directory" % logpath)
r = Redis()
for logfile in [x for x in listdir(logpath) if not isdir(join(logpath,x))]:
logger.debug("Parsing %s" % logfile)
with open(join(logpath,logfile), "r") as loghandle:
for logline in loghandle:
logger.debug("Passing log line to queue: %s" % logline)
pl = parseline(logline)
# make sure it is a message from the wsgi handler
if pl[3] == "[wsgi]":
r.lpush(out_queue, logline)
# crude rate limiting
if r.llen(out_queue) > 10000:
logger.info("Rate limiting as there are 10000 msgs on the queue already. Sleep for 1 sec")
sleep(0.1)
if r.llen(out_queue) > 1000000:
logger.info("Rate limiting as there are 100000 msgs on the queue already! Sleep for 10 sec")
sleep(10)