-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbLog.py
59 lines (52 loc) · 2.44 KB
/
DbLog.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
import os
import datetime as dt
#import inspect # For getting code info from calling script
import sys # Works better
import ast
from rich import print
LOG_FILENAME = "DB_MANAGER_LOG.log"
RECURSION_STOPS = []
EXCLUSIONS = ["__init__", "execute_query", "execute_read_query"]
# Get all function names from command for stopping backwards frame recursion.
filename = "command.py"
with open(filename) as file:
node = ast.parse(file.read())
classes = [n for n in node.body if isinstance(n, ast.ClassDef)]
for class_ in classes:
RECURSION_STOPS.extend([n.name for n in class_.body if isinstance(n, ast.FunctionDef)])
RECURSION_STOPS = [item for item in RECURSION_STOPS if item not in EXCLUSIONS]
filename = "database_manager.py"
with open(filename) as file:
node = ast.parse(file.read())
classes = [n for n in node.body if isinstance(n, ast.ClassDef)]
for class_ in classes:
RECURSION_STOPS.extend([n.name for n in class_.body if isinstance(n, ast.FunctionDef)])
RECURSION_STOPS = [item for item in RECURSION_STOPS if item not in EXCLUSIONS]
class DbLog():
"""Log to log_file."""
def __init__(self) -> None:
self.log_file_path = os.path.join(os.getcwd(), LOG_FILENAME)
self.debug_print = False
self.write_to_file = True
def log(self, *data, err=False, reason=None):
"""Log any data"""
data = [str(d) for d in data]
if self.debug_print:
print(' '.join(data))
if "Query executed successfully" in data:
# Shorten.
data = ["Q.e.s."]
if self.write_to_file:
with open(self.log_file_path, 'a') as log:
log.write(f"\nDB_MANAGER_LOG ({dt.datetime.now().strftime('%H:%M:%S %m/%d/%Y')}): "
+ ("ERROR " if err else "") + "*" + ' '.join(data) + "* ")
if reason:
# Get filename together with function name of calling script while recursively going through until recursion stop is hit.
frame = sys._getframe()
last_frame = frame
while not frame.f_code.co_name in RECURSION_STOPS and frame:
last_frame = frame
frame = frame.f_back
if not frame:
frame = last_frame
log.write(f"--> REASON \"{reason}\" | From file \"{os.path.basename(frame.f_code.co_filename)}\" in func \"{frame.f_code.co_name}\", lineno:{frame.f_lineno}")