-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogger.py
More file actions
114 lines (91 loc) · 3.13 KB
/
Copy pathLogger.py
File metadata and controls
114 lines (91 loc) · 3.13 KB
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
from enum import auto
from BotEnums import CompareEnum
import datetime, time, asyncio, sys, coloredlogs
from logger_tt import setup_logging, logger
__all__ = ["LogLevel", "Logger"]
class LogLevel(CompareEnum):
Debug=auto()
Verbose=auto()
Log=auto()
Warn=auto()
Error=auto()
Notice=auto()
Silence=auto()
CurrentLoggingLevel = LogLevel.Debug
CurrentNotificationLevel = LogLevel.Warn
HasInitialized = False
NotificationCallback = None
coloredlogs.DEFAULT_LOG_FORMAT = '[%(asctime)s] %(processName)-24s %(levelname)9s %(message)s'
coloredlogs.DEFAULT_LEVEL_STYLES = {'critical': {'bold': True, 'color': 'red'}, 'debug': {'color': 'green'}, 'error': {'color': 'red'}, 'info': {}, 'notice': {'color': 'magenta'}, 'spam': {'color': 'green', 'faint': True}, 'success': {'bold': True, 'color': 'green'}, 'verbose': {'color': 'blue'}, 'warning': {'color': 'yellow'}}
coloredlogs.DEFAULT_FIELD_STYLES = {}
setup_logging(config_path='log_config.json')
coloredlogs.install(level='VERBOSE')
class Logger():
@staticmethod
def Start():
global HasInitialized
if (not HasInitialized):
HasInitialized = True
@staticmethod
def GetTimestamp():
return time.time()
@staticmethod
def PrintDate():
NowTime = str(datetime.datetime.now())
return f"[{NowTime}] "
@staticmethod
def CLog(Conditional, Level:LogLevel, Input:str):
ShouldPrint:bool = False
try:
if (callable(Conditional)):
ShouldPrint = Conditional() # pyright: ignore[reportAssignmentType]
else:
ShouldPrint = bool(Conditional)
except Exception as ex:
logger.debug(f"Encountered error on conditional check {ex}")
return
if (ShouldPrint):
Logger.Log(Level, Input)
@staticmethod
def Log(Level:LogLevel, Input:str):
global NotificationCallback
if Level < CurrentLoggingLevel:
return
if CurrentLoggingLevel == LogLevel.Silence:
return
# Set up color logging
ColorStr = ""
LoggerFunc = logger.info
MessageStr = f"[{sys._getframe(1).f_code.co_name}] {Input}"
if Level == LogLevel.Error:
LoggerFunc = logger.error
elif Level == LogLevel.Warn:
LoggerFunc = logger.warning
elif Level == LogLevel.Verbose:
LoggerFunc = logger.info
elif Level == LogLevel.Debug:
LoggerFunc = logger.debug
LoggerFunc(f"{MessageStr}")
if (NotificationCallback is not None and Level >= CurrentNotificationLevel):
try:
CurrentLoop = asyncio.get_running_loop()
except RuntimeError:
# If there is no currently running loop, then don't bother sending notification messages
return
# This will automatically get added to the task loop.
CurrentLoop.create_task(NotificationCallback(f"[{str(Level)}]: {MessageStr}"))
@staticmethod
def SetLogLevel(NewLevel: LogLevel):
global CurrentLoggingLevel
CurrentLoggingLevel = NewLevel
@staticmethod
def GetLogLevel():
return CurrentLoggingLevel
@staticmethod
def GetLogLevelName():
return CurrentLoggingLevel.name
@staticmethod
def SetNotificationCallback(NewCallback):
global NotificationCallback
NotificationCallback = NewCallback
Logger.Start()