-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Logger.py
100 lines (79 loc) · 2.76 KB
/
Logger.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
from enum import auto
from EnumWrapper import CompareEnum
import datetime, time, asyncio, sys
from logger_tt import setup_logging, logger
import coloredlogs
__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 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.warn
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()