forked from samwize/python-email-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorStreamHandler.py
executable file
·68 lines (57 loc) · 1.63 KB
/
ColorStreamHandler.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
import logging
import curses
class ColorStreamHandler(logging.Handler):
def __init__(self, use_colors):
logging.Handler.__init__(self)
self.use_colors = use_colors
# Initialize environment
curses.setupterm()
# Get the foreground color attribute for this environment
self.fcap = curses.tigetstr('setaf')
#Get the normal attribute
self.COLOR_NORMAL = curses.tigetstr('sgr0')
# Get + Save the color sequences
self.COLOR_INFO = curses.tparm(self.fcap, curses.COLOR_GREEN)
self.COLOR_ERROR = curses.tparm(self.fcap, curses.COLOR_RED)
self.COLOR_WARNING = curses.tparm(self.fcap, curses.COLOR_YELLOW)
self.COLOR_DEBUG = curses.tparm(self.fcap, curses.COLOR_BLUE)
def color(self, msg, level):
if level == "INFO":
return "%s%s%s" % (self.COLOR_INFO, msg, self.COLOR_NORMAL)
elif level == "WARNING":
return "%s%s%s" % (self.COLOR_WARNING, msg, self.COLOR_NORMAL)
elif level == "ERROR":
return "%s%s%s" % (self.COLOR_ERROR, msg, self.COLOR_NORMAL)
elif level == "DEBUG":
return "%s%s%s" % (self.COLOR_DEBUG, msg, self.COLOR_NORMAL)
else:
return msg
def emit(self, record):
record.msg = record.msg.encode('utf-8', 'ignore')
msg = self.format(record)
# This just removes the date and milliseconds from asctime
temp = msg.split(']')
msg = '[' + temp[0].split(' ')[1].split(',')[0] + ']' + temp[1]
if self.use_colors:
msg = self.color(msg, record.levelname)
print msg
# 'record' has the following attributes:
# threadName
# name
# thread
# created
# process
# processName
# args
# module
# filename
# levelno
# exc_text
# pathname
# lineno
# msg
# exc_info
# funcName
# relativeCreated
# levelname
# msecs