-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogger.py
More file actions
59 lines (48 loc) · 1.98 KB
/
logger.py
File metadata and controls
59 lines (48 loc) · 1.98 KB
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
import logging
import sys
from termcolor import colored
class _MyFormatter(logging.Formatter):
def format(self, record):
date = colored('[%(asctime)s @%(filename)s:%(lineno)d]', 'green')
msg = '%(message)s'
if record.levelno == logging.WARNING:
fmt = date + ' ' + colored('WRN', 'red', attrs=['blink']) + ' ' + msg
elif record.levelno == logging.ERROR or record.levelno == logging.CRITICAL:
fmt = date + ' ' + colored('ERR', 'red', attrs=['blink', 'underline']) + ' ' + msg
else:
fmt = date + ' ' + msg
if hasattr(self, '_style'):
# Python3 compatibilty
self._style._fmt = fmt
self._fmt = fmt
return super(_MyFormatter, self).format(record)
class ConsoleHandler(logging.StreamHandler):
"""A handler that logs to console in the sensible way.
StreamHandler can log to *one of* sys.stdout or sys.stderr.
It is more sensible to log to sys.stdout by default with only error
(logging.ERROR and above) messages going to sys.stderr. This is how
ConsoleHandler behaves.
"""
def __init__(self):
logging.StreamHandler.__init__(self)
self.stream = None # reset it; we are not going to use it anyway
def emit(self, record):
if record.levelno >= logging.ERROR:
self.__emit(record, sys.stderr)
else:
self.__emit(record, sys.stdout)
def __emit(self, record, strm):
self.stream = strm
logging.StreamHandler.emit(self, record)
def flush(self):
# Workaround a bug in logging module
# See:
# http://bugs.python.org/issue6333
if self.stream and hasattr(self.stream, 'flush') and not self.stream.closed:
logging.StreamHandler.flush(self)
logger = logging.getLogger('paperhero')
logger.propagate = False
logger.setLevel(logging.INFO)
handler = ConsoleHandler()
handler.setFormatter(_MyFormatter(datefmt='%m%d %H:%M:%S'))
logger.addHandler(handler)