-
Notifications
You must be signed in to change notification settings - Fork 24
/
logger.py
49 lines (38 loc) · 1.25 KB
/
logger.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
import logging
import getpass
from datetime import datetime
from socket import gethostname
from backend.mongodb import MongoBackend
backend = MongoBackend()
class MongoFormatter(logging.Formatter):
def format(self, record):
"""Format exception object as a string"""
data = record.__dict__.copy()
if record.args:
msg = record.msg % record.args
else:
msg = record.msg
data.update(
username=getpass.getuser(),
time=datetime.utcnow(),
host=gethostname(),
msg=msg,
args=tuple(unicode(arg) for arg in record.args)
)
if 'exc_info' in data and data['exc_info']:
data['exc_info'] = self.formatException(data['exc_info'])
return data
class MongodbHandler(logging.Handler):
def __init__(self, level=logging.NOTSET):
logging.Handler.__init__(self, level)
self.formatter = MongoFormatter()
def emit(self, record):
msg = self.format(record)
backend.insert_logmsg(msg)
logging.basicConfig(
format='[%(asctime)s] %(message)s',
level=logging.DEBUG)
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
if not log.handlers:
log.addHandler(MongodbHandler())