-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloggingSetup.py
More file actions
39 lines (30 loc) · 1000 Bytes
/
loggingSetup.py
File metadata and controls
39 lines (30 loc) · 1000 Bytes
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
import os
import logging
import sys
class ColorHandler(logging.StreamHandler):
# https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
GRAY8 = "38;5;8"
PURPLE = "35"
ORANGE = "33"
RED = "31"
WHITE = "0"
def emit(self, record):
# We don't use white for any logging, to help distinguish from user print statements
level_color_map = {
logging.DEBUG: self.GRAY8,
logging.INFO: self.PURPLE,
logging.WARNING: self.ORANGE,
logging.ERROR: self.RED,
}
csi = f"{chr(27)}[" # control sequence introducer
color = level_color_map.get(record.levelno, self.WHITE)
self.stream.write(f"{csi}{color}m{record.msg}{csi}m\n\n")
self.setStream(sys.stdout)
logger = logging.getLogger(__name__)
logger.setLevel(10)
# See https://no-color.org/
if not os.environ.get("NO_COLOR"):
logging.getLogger(__name__).addHandler(ColorHandler())
file_handler = logging.FileHandler("vocabChecker.log", mode="a", encoding="utf-8")
logger.addHandler(file_handler)
# TODO logging print is delayed