Skip to content

Commit

Permalink
Update algopy.py to add a new Log class replacing old one
Browse files Browse the repository at this point in the history
  • Loading branch information
DefinetlyNotAI authored Aug 12, 2024
1 parent 61df7fc commit 703856c
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions algopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,25 @@


class Log:
def __init__(self, filename="Server.log", max_size=None):
def __init__(self, filename="Server.log"):
"""
Initializes a new instance of the Log class.
Args:
filename (str, optional): The name of the log file. Defaults to "Server.log".
max_size (int, optional): The maximum size of the log file in bytes. Defaults to infinity.
Initializes the `filename` and `size` attributes of the Log instance.
If the log file does not exist, it creates an empty file with the specified name.
"""
# Use the provided filename or default to 'Server.log'
self.filename = str(filename)
self.size = int(max_size)

# Check if the file exists and create it if it doesn't
if not os.path.exists(self.filename):
with open(self.filename, "w"):
with open(self.filename, "w") as log_file:
log_file.write(
"|-----Timestamp-----|--Log Level--|-----------------------------------------------------------------------Log Messages-----------------------------------------------------------------------|\n"
)
pass # Empty file content is fine here since we append logs

@staticmethod
Expand All @@ -128,27 +129,10 @@ def __timestamp():
"""
# Get the current date and time
now = datetime.now()

# Format the timestamp as a string
time = f"{now.strftime('%Y-%m-%d %H:%M:%S')}"

return time

def __remove(self):
"""
Remove the log file if it exists and the number of lines in the file exceeds the specified size.
This function checks if the log file specified by the `filename` attribute exists. If it does, it opens the file in read mode and counts the number of lines in the file. If the number of lines is greater than the specified `size`, the file is removed.
Returns:
None
"""
if os.path.exists(self.filename) and self.size is not None:
with open(self.filename, "r") as file:
line_count = sum(1 for _ in file)
if line_count > self.size:
os.remove(self.filename)

def info(self, message):
"""
Writes an information log message to the log file.
Expand All @@ -159,9 +143,8 @@ def info(self, message):
Returns:
None
"""
self.__remove()
with open(self.filename, "a") as f:
f.write(f"INFO: {message} at {self.__timestamp()}\n")
f.write(f"[{self.__timestamp()}] > INFO: {message}\n")

def warning(self, message):
"""
Expand All @@ -173,9 +156,8 @@ def warning(self, message):
Returns:
None
"""
self.__remove()
with open(self.filename, "a") as f:
f.write(f"WARNING: {message} at {self.__timestamp()}\n")
f.write(f"[{self.__timestamp()}] > WARNING: {message}\n")

def error(self, message):
"""
Expand All @@ -187,9 +169,8 @@ def error(self, message):
Returns:
None
"""
self.__remove()
with open(self.filename, "a") as f:
f.write(f"ERROR: {message} at {self.__timestamp()}\n")
f.write(f"[{self.__timestamp()}] > ERROR: {message}\n")

def critical(self, message):
"""
Expand All @@ -201,9 +182,8 @@ def critical(self, message):
Returns:
None
"""
self.__remove()
with open(self.filename, "a") as f:
f.write(f"CRITICAL: {message} at {self.__timestamp()}\n")
f.write(f"[{self.__timestamp()}] > CRITICAL: {message}\n")


class Find:
Expand Down

0 comments on commit 703856c

Please sign in to comment.