-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstash.py
35 lines (30 loc) · 1.17 KB
/
stash.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
import logging
from pyes import ES
import datetime
class StashHandler(logging.Handler):
def __init__(self, constr, whitelist=None, blacklist=None):
logging.Handler.__init__(self)
self.conn = ES(constr)
if blacklist is None:
blacklist = set()
self.whitelist = whitelist
self.blacklist = blacklist
self.record_type = 'record'
@property
def index_name(self):
return 'logstash-'+datetime.date.today().strftime('%Y.%m.%d')
def emit(self, record):
if self.whitelist is None:
d = { k: record.__dict__[k] for k in record.__dict__ if k not in self.blacklist }
else:
d = { k: record.__dict__[k] for k in record.__dict__ if k in self.whitelist and k not in self.blacklist }
entry = {
"@fields": d,
"@message": record.msg,
"@source": "gelf://localhost",
"@source_host": "gelf://localhost",
"@source_path": "/",
"@tags": [],
"@timestamp": datetime.datetime.utcnow().isoformat(),
"@type": self.record_type}
self.conn.index(entry, self.index_name, self.record_type)