-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_formatter.py
67 lines (52 loc) · 1.95 KB
/
custom_formatter.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
import logging
class CustomFormatter(logging.Formatter):
"""Logging Formatter to add colors and count warning / errors"""
grey = "\x1b[38;21m"
bold_green = "\x1b[32;1m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
fmt = "%(message)s"
#%(levelname)s %(asctime)s - %(name)s - (%(filename)s:%(lineno)d)
FORMATS = {
logging.DEBUG: bold_green + "[D] " + fmt + reset,
logging.INFO: bold_green + "[+] " + fmt + reset,
logging.WARNING: yellow + "[?] " + fmt + reset,
logging.ERROR: bold_red + "[!] " + fmt + reset,
logging.CRITICAL: bold_red + "[/] " + fmt + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
def log_debug(self, message):
logger = logging.getLogger("Cipherhound")
# logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
logger.propagate = False
logger.addHandler(ch)
logger.debug(message)
logger.removeHandler(ch)
def log_error(self, message):
logger = logging.getLogger("Cipherhound")
logger.setLevel(logging.ERROR)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
ch.setFormatter(CustomFormatter())
logger.propagate = False
logger.addHandler(ch)
logger.error(message)
logger.removeHandler(ch)
def log_info(self, message):
logger = logging.getLogger("Cipherhound")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(CustomFormatter())
logger.propagate = False
logger.addHandler(ch)
logger.info(message)
logger.removeHandler(ch)