-
Notifications
You must be signed in to change notification settings - Fork 2
/
loggerbase.py
executable file
·63 lines (50 loc) · 1.96 KB
/
loggerbase.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
#!/usr/bin/env python3
""" Class for all things around logging """
from colorama import Fore as ansi
import time
__all__ = [ "LoggerBase" ]
class LoggerBase:
__slots__ = [ "bakerid", "module" ]
def __init__ ( self, bakerid : int = 0 ):
""" instantiate the logger class with a bakerid """
self.bakerid = bakerid
module = str(type(self)).replace("<class '","").replace("'>","")
p1 = module.find(".")
if module.count(".")==2:
p2 = module.rfind(".")
self.module = module[p1+1:p2]
else:
self.module = module[p1+1:]
def error ( self, *args ):
self.highlight ( "error", *args )
def info ( self, *args ):
""" logging to file, but also write to screen """
self.log ( *args )
if self.verbose > 1:
print ( "[self.module] %s" % ( " ".join(map(str,args)) ) )
def highlight ( self, msgType : str = "info", *args ):
""" logging, hilit """
col = ansi.GREEN
if msgType.lower() in [ "error", "red" ]:
col = ansi.RED
elif msgType.lower() in [ "warn", "warning", "yellow" ]:
col = ansi.YELLOW
elif msgType.lower() in [ "green", "info" ]:
col = ansi.GREEN
else:
self.highlight ( "red", "I think we called highlight without msg type" )
print ( f'{col}[{self.module}:{time.strftime("%H:%M:%S")}] {" ".join(map(str,args))}{ansi.RESET}' )
self.log ( *args )
def debug ( self, *args ):
pass
def pprint ( self, *args ):
""" logging """
print ( f"[{self.module}:{self.bakerid}] {' '.join(map(str,args))}" )
self.log ( *args )
def log ( self, *args ):
""" logging to file """
with open( f"emcreator.log", "a" ) as f:
f.write ( f'[{self.module}-{time.strftime("%H:%M:%S")}] {" ".join(map(str,args))}\n' )
if __name__ == "__main__":
logger = LoggerBase ( 0 )
logger.pprint ( "what now!" )