-
Notifications
You must be signed in to change notification settings - Fork 1
/
module_logging.py
116 lines (89 loc) · 3.46 KB
/
module_logging.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: utf-8 -*-
# PYTHON BUILTINS
import logging # self-explanatory ;)
from pathlib import Path # for interacting with the filesystem
import textwrap # for aligning the log message into a column
import traceback # for detailed error handling
class WrappedFixedIndentingLog(logging.Formatter):
def __init__(self, fmt=None, datefmt=None, style='%', width=176, indent=46):
super().__init__(fmt=fmt, datefmt=datefmt, style=style)
self.wrapper = textwrap.TextWrapper(width=width, subsequent_indent=' '*indent)
def format(self, record):
return self.wrapper.fill(super().format(record))
class ModuleFilterMain(logging.Filter):
def filter(self, record):
record.module_tag = "[MAIN]:"
return True
class ModuleFilterDRV(logging.Filter):
def filter(self, record):
record.module_tag = "[DRV]:"
return True
class ModuleFilterDB(logging.Filter):
def filter(self, record):
record.module_tag = "[DB]:"
return True
class ModuleFilterREQ(logging.Filter):
def filter(self, record):
record.module_tag = "[REQ]:"
return True
class ModuleFilterTG(logging.Filter):
def filter(self, record):
record.module_tag = "[TG]:"
return True
class ModuleFilterVPN(logging.Filter):
def filter(self, record):
record.module_tag = "[VPN]:"
return True
class ModuleFilterGeneric(logging.Filter):
def filter(self, record):
record.module_tag = "[OTHER]:"
return True
def create_logger(module):
# create logger
logger = logging.getLogger(str(module) + "_log")
logger.setLevel(logging.DEBUG)
# create logs folder in project tree if folder does not already exist
Path("./_logs").mkdir(parents=True, exist_ok=True)
# create file handler and set it to DEBUG level
# fh1 = logging.FileHandler("_logs/" + str(module) + "_debug.log")
# fh1.setLevel(logging.DEBUG)
# create file handler and set it to INFO level
fh2 = logging.FileHandler("_logs/" + str(module) + "_info.log")
fh2.setLevel(logging.INFO)
# create file handler and set it to WARNING level
fh3 = logging.FileHandler("_logs/" + str(module) + "_warning.log")
fh3.setLevel(logging.WARNING)
# create console handler and set it to DEBUG level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# add filter depending on the module
if module == "main":
logger.addFilter(ModuleFilterMain())
elif module == "db":
logger.addFilter(ModuleFilterDB())
elif module == "drv":
logger.addFilter(ModuleFilterDRV())
elif module == "req":
logger.addFilter(ModuleFilterREQ())
elif module == "tg":
logger.addFilter(ModuleFilterTG())
elif module == "vpn":
logger.addFilter(ModuleFilterVPN())
else:
logger.addFilter(ModuleFilterGeneric())
# create (custom) formatter and add it to the handlers
formatter = WrappedFixedIndentingLog("%(asctime)s - %(levelname)-8s - %(module_tag)-8s %(message)s", width=176, indent=46)
# fh1.setFormatter(formatter)
fh2.setFormatter(formatter)
fh3.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
# logger.addHandler(fh1)
logger.addHandler(fh2)
logger.addHandler(fh3)
logger.addHandler(ch)
return logger
def exception_printing(exc):
exctype = type(exc).__name__
tb = "\n".join(traceback.format_tb(exc.__traceback__))
return exctype, str(exc), str(tb)