diff options
Diffstat (limited to 'logger.py')
| -rw-r--r-- | logger.py | 13 |
1 files changed, 12 insertions, 1 deletions
@@ -1,6 +1,7 @@ import time from datetime import datetime import pytz +import os def _get_datetime_string(): utc_now = datetime.now(pytz.timezone('UTC')) @@ -30,4 +31,14 @@ class Logger: def log(self, message: str): with open(self.path, "a") as file: - file.write(f"[{_get_datetime_string()}] {message}\n")
\ No newline at end of file + self.check_log_size() + file.write(f"[{_get_datetime_string()}] {message}\n") + + def check_log_size(self): + if os.path.getsize(self.path) > self.max_size_bytes: + with open(self.path, "r") as file: + lines = file.readlines() + with open(self.path, "w") as file: + file.writelines(lines[int(len(lines) / 2):]) + file.write("Removed half of the log file due to size\n") + |
