commit 3c65bc05b89e83bde3ed170a2faffeec5ff4347c Author: James Hoffman Date: Wed Jan 3 11:27:05 2024 -0700 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..b4608c3 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +## A super simple logger function +Only here so I can clone it for simple logging. diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..84fcd51 --- /dev/null +++ b/__init__.py @@ -0,0 +1,10 @@ +import os, datetime + +if os.path.isfile("logs/main.log"): + if os.path.getsize("logs/main.log") >= 128000: + ct = datetime.datetime.now() + os.rename("logs/main.log", "logs/"+str(ct).replace(" ", "-")+".log") +else: + if not os.path.exists("logs"): + os.makedirs("logs") + f = open("logs/main.log", "w").close() diff --git a/simpleLog.py b/simpleLog.py new file mode 100644 index 0000000..ed144f2 --- /dev/null +++ b/simpleLog.py @@ -0,0 +1,44 @@ +import datetime, os +from inspect import getframeinfo, stack +i="" #Stop ide complaining + +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + +def log(level, message, thread="", detail=True, write=True): + global i + message = f"{bcolors.OKCYAN}{message}{bcolors.ENDC}" + if thread != "": + thread = f"{bcolors.OKGREEN}{thread}{bcolors.ENDC}"+":" + if level.lower() == "i": + i = f"{bcolors.HEADER}INFO:{bcolors.ENDC}" + level = "INFO:" + if level.lower() == "w": + message = f"{bcolors.WARNING}{message.strip(bcolors.OKCYAN).strip(bcolors.ENDC)}{bcolors.ENDC}" + i = f"{bcolors.WARNING}WARNING:{bcolors.ENDC}" + level = "WARNING:" + if level.lower() == "e": + message = f"{bcolors.FAIL}{bcolors.BOLD}{message.strip(bcolors.OKCYAN).strip(bcolors.ENDC)}{bcolors.ENDC}" + i = f"{bcolors.FAIL}{bcolors.BOLD}ERROR:{bcolors.ENDC}" + level = "ERROR:" + if detail: + caller = getframeinfo(stack()[1][0]) + name = caller.filename.replace(os.getcwd()+"/", "") + e = i + name +":"+f"{bcolors.OKGREEN}{str(caller.lineno)}{bcolors.ENDC}"+":"+thread+" "+message + f = level+name+" "+str(caller.lineno)+":"+thread.strip(bcolors.OKGREEN).strip(bcolors.ENDC).strip(bcolors.WARNING).strip(bcolors.FAIL)+" "+message.strip(bcolors.OKCYAN).strip(bcolors.ENDC).strip(bcolors.WARNING).strip(bcolors.FAIL) + else: + e = i+thread+" "+message + f = level+thread.strip(bcolors.OKGREEN).strip(bcolors.ENDC).strip(bcolors.WARNING).strip(bcolors.FAIL)+" "+message.strip(bcolors.OKCYAN).strip(bcolors.OKGREEN).strip(bcolors.ENDC).strip(bcolors.WARNING).strip(bcolors.FAIL) + print(e) + if write: + logF = open("logs/main.log", "a") + logF.write(str(datetime.datetime.now())+":"+f.replace(bcolors.ENDC, "")+"\n") + logF.close() diff --git a/test.py b/test.py new file mode 100644 index 0000000..c17c20a --- /dev/null +++ b/test.py @@ -0,0 +1,25 @@ +from simpleLog import log +import os + +os.remove("logs/main.log") +#Test 1 +print("Everything should look like how you want it to.\n") +log("i", "Testing...") +log("i", "I should have a thread listed.", "otherThread") +log("w", "Warning...") +log("w", "I should have a thread listed.", "otherThread") +log("e", "Error!") +log("e", "I should have a thread listed.", "otherThread") +print("\nYou should stop seeing any details!\n") +log("i", "Testing...", "", False) +log("i", "I should have a thread listed.", "otherThread",False) +log("w", "Warning...", "", False) +log("w", "I should have a thread listed.", "otherThread", False) +log("e", "Error!", "", False) +log("e", "I should have a thread listed.", "otherThread", False) +print("\nYou should not see the following in the log file!\n") +log("i", "I should not appear in the log file!", "", False, False) +log("i", "I should not appear in the log file!", "", True, False) + +print("\nMake sure the log file doesn't have the above or any color codes!") +