initial commit

This commit is contained in:
James Hoffman 2024-01-03 11:27:05 -07:00
commit 3c65bc05b8
4 changed files with 81 additions and 0 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
## A super simple logger function
Only here so I can clone it for simple logging.

10
__init__.py Normal file
View File

@ -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()

44
simpleLog.py Normal file
View File

@ -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()

25
test.py Normal file
View File

@ -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!")