-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_utils.py
55 lines (44 loc) · 1.69 KB
/
log_utils.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
import logging
# Code from https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
# This formatter is for pretty logs
class Formatter(logging.Formatter):
grey = "\x1b[37m"
yellow = "\x1b[33m"
red = "\x1b[31m"
bold_red = "\x1b[1;31m"
reset = "\x1b[0m"
format = "%(asctime)s - %(name)-24s - %(levelname)-7s - %(message)s (%(filename)s:%(lineno)d)"
FORMATS = {
logging.DEBUG: logging.Formatter(grey + format + reset),
logging.INFO: logging.Formatter(format),
logging.WARNING: logging.Formatter(yellow + format + reset),
logging.ERROR: logging.Formatter(red + format + reset),
logging.CRITICAL: logging.Formatter(bold_red + format + reset)
}
def format(self, record):
return self.FORMATS.get(record.levelno).format(record)
# Saves logs to a variable until said variable is wiped
logs = []
class VariableHandler(logging.Handler):
def __init__(self):
super().__init__(logging.DEBUG)
def emit(self, record):
logs.append(format(record))
# Initialize logging config
def setup_logging():
# Setup color logging,
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(Formatter())
varHandler = VariableHandler()
logging.basicConfig(
level=logging.DEBUG,
handlers=[ch, varHandler]
)
logging.info("Setup Logging")
# No Spammy Loggers
logging.getLogger("watcher").setLevel(logging.INFO)
logging.getLogger("paramiko").setLevel(logging.INFO)
logging.getLogger("pyvisa").setLevel(logging.INFO)
logging.getLogger("daqController").setLevel(logging.INFO)
logging.getLogger("inotifierHandler").setLevel(logging.INFO)