Full completed project
This commit is contained in:
1
server/modules/track/__init__.py
Normal file
1
server/modules/track/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__all__ = ['logging']
|
||||
BIN
server/modules/track/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
server/modules/track/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/modules/track/__pycache__/logging.cpython-311.pyc
Normal file
BIN
server/modules/track/__pycache__/logging.cpython-311.pyc
Normal file
Binary file not shown.
109
server/modules/track/logging.py
Normal file
109
server/modules/track/logging.py
Normal file
@@ -0,0 +1,109 @@
|
||||
from datetime import datetime
|
||||
from os.path import exists
|
||||
|
||||
class log():
|
||||
def __init__(self, level, message):
|
||||
if not hasattr(self, "message_type"):
|
||||
self.message_type = "log"
|
||||
self.time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
self.level = level
|
||||
self.message = message
|
||||
self.path = "data/log.txt"
|
||||
|
||||
if self.message_type == "log":
|
||||
self._create()
|
||||
|
||||
def log_file_exist(self):
|
||||
file_exists = exists(self.path)
|
||||
return file_exists
|
||||
|
||||
def create(self):
|
||||
# here for legacy support
|
||||
# old version required a specific call to log(*info).create
|
||||
# this has since been revambed
|
||||
pass
|
||||
|
||||
def _create(self, log_string=None):
|
||||
if not log_string:
|
||||
log_string = f"{self.time} | {self.level} | {self.message}"
|
||||
|
||||
if not self.log_file_exist():
|
||||
with open(self.path, 'w') as log_file:
|
||||
log_file.write(f"{self.time} | INFO | Log file created at '{self.path}'")
|
||||
else:
|
||||
with open(self.path, 'a') as log_file:
|
||||
log_file.write(log_string)
|
||||
self.output(log_string)
|
||||
|
||||
def read(self, amount):
|
||||
with open(self.path, 'r') as log_file:
|
||||
entries = log_file.readlines()
|
||||
if amount == None:
|
||||
return entries
|
||||
entries = entries[len(entries)-amount:]
|
||||
return entries
|
||||
|
||||
def output(self, log_string):
|
||||
if self.message_type == "log":
|
||||
print(log_string)
|
||||
|
||||
class status(log):
|
||||
def __init__(self, level, message, interface=None):
|
||||
self.message_type = "status"
|
||||
super().__init__(level, message)
|
||||
self.status = {"time":self.time, "level":self.level, "message":self.message}
|
||||
if interface:
|
||||
self.interface = interface
|
||||
self.process()
|
||||
|
||||
# LEGACY METHODS
|
||||
def status_update(self, obj):
|
||||
status = {"time":self.time, "level":self.level, "message":self.message}
|
||||
if obj != None:
|
||||
obj.status = status
|
||||
obj.status_string = f"{self.time} | {self.level} | {self.message}"
|
||||
|
||||
return status
|
||||
|
||||
@staticmethod
|
||||
def send_status(sio, sid, status):
|
||||
sio.emit('recv_status', status, room=sid)
|
||||
# LEGACY METHODS
|
||||
|
||||
def process(self):
|
||||
self.__format()
|
||||
self.__object_update()
|
||||
|
||||
self._create(self.log_string)
|
||||
self.interface.send_status(self.status)
|
||||
|
||||
def __object_update(self):
|
||||
if self.interface.obj != None:
|
||||
self.interface.obj.status = self.status
|
||||
self.interface.obj.status_string = self.status_string
|
||||
|
||||
def __format(self):
|
||||
self.status = {"time":self.time, "level":self.level, "message":self.message}
|
||||
self.status_string = f"{self.time} | {self.level} | {self.message}"
|
||||
|
||||
user_id = self.interface.user_id
|
||||
sid = self.interface.sid
|
||||
self.log_string = f"{self.time} | {self.level} | {self.interface.user_id} | {self.message}"
|
||||
|
||||
class status_interface(log):
|
||||
def __init__(self, sio, sid, user_id="Unknown", obj=None):
|
||||
self.sio = sio
|
||||
self.sid = sid
|
||||
self.user_id = user_id
|
||||
self.obj = obj
|
||||
self.path = "data/actions_log.txt"
|
||||
|
||||
def send_status(self, status):
|
||||
self.sio.emit('recv_status', status, room=self.sid)
|
||||
|
||||
def main():
|
||||
entry = logging("INFO", "test log creation")
|
||||
entry.path = "log.txt"
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user