forked from joekickass/python-kinesis-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinesishandler.py
39 lines (31 loc) · 954 Bytes
/
kinesishandler.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
#
# Copyright (C) 2016 Tomas Nilsson (joekickass). All rights reserved.
#
from logging.handlers import BufferingHandler
class KinesisHandler(BufferingHandler):
"""
Sends logs in batches to Kinesis
Uses a queue to dispatch batched data to worker thread
"""
def __init__(self, capacity, queue):
"""
Initialize the handler with buffer size and queue
"""
BufferingHandler.__init__(self, capacity)
self.queue = queue
def prepare(self, records):
"""
Prepare data for queuing
TODO: Is self.format() keeping all info? what about errors?
"""
return [self.format(record) for record in records]
def flush(self):
"""
Put buffered data in queue and zap buffer.
"""
self.acquire()
try:
self.queue.put(self.prepare(self.buffer))
self.buffer = []
finally:
self.release()