forked from emfcamp/Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
65 lines (52 loc) · 1.5 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
# Ansi color codes
CSI = "\x1b["
RESET = 0
BOLD = 1
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30, 38)
BACKGROUND = 40 - 30
def sgr(*codes):
return CSI + ";".join(map(str, codes)) + "m"
# modified from http://plumberjack.blogspot.co.uk/2010/12/colorizing-logging-output-in-terminals.html
class ColorizingStreamHandler(logging.StreamHandler):
DEBUG = sgr(BLUE)
WARNING = sgr(YELLOW)
ERROR = sgr(RED)
CRITICAL = sgr(RED, BOLD)
reset = sgr(RESET)
@property
def is_tty(self):
isatty = getattr(self.stream, "isatty", None)
return isatty and isatty()
def emit(self, record):
try:
message = self.format(record)
self.stream.write(message + "\n")
self.flush()
except Exception:
self.handleError(record)
def colorize(self, message, record):
color = getattr(self, record.levelname, None)
if color:
message = color + message + self.reset
return message
def format(self, record):
message = logging.StreamHandler.format(self, record)
if self.is_tty:
message = self.colorize(message, record)
return message
class GreenStreamHandler(ColorizingStreamHandler):
INFO = sgr(GREEN)
def mail_logging(message, app):
msg = u"""
+++++ SENDING MAIL +++++
TO: {0.recipients}
FROM: {0.sender}
SUBJECT: {0.subject}
---------
{0.body}
++++++++++
""".format(
message
)
app.logger.info(msg)