From be8a4fdcd545db3280a4af109c31bd458bbf17dd Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:01:52 +0530 Subject: [PATCH 1/8] Make imports easier to read --- utils/logger.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils/logger.py b/utils/logger.py index 6301b047..df949d21 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -1,4 +1,7 @@ -import time, datetime +# Imports +import time +import datetime + log_path = "logs/info-log.txt" error_path = "logs/error-log.txt" class colours: From eb1cb0675c457053f52c76d0768459216688ea0e Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:02:20 +0530 Subject: [PATCH 2/8] Add escape code for `warning` text color Basically orange or yellow --- utils/logger.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/logger.py b/utils/logger.py index df949d21..29a45c84 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -7,6 +7,7 @@ class colours: error = '\033[91m' success = '\033[92m' + warning = '\033[33m' end = '\033[0m' def info(text:str, *, nolog=False): current_time = datetime.datetime.now().strftime("%H:%M:%S") From 9a03a4f6e243dbf7bc19ceaa2715fe4cf1365c65 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:03:28 +0530 Subject: [PATCH 3/8] Improve logging system for `logger.info()` --- utils/logger.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/utils/logger.py b/utils/logger.py index 29a45c84..306f0072 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -9,9 +9,17 @@ class colours: success = '\033[92m' warning = '\033[33m' end = '\033[0m' -def info(text:str, *, nolog=False): + +# Main Functions +def info(text:str, *, nolog=False, module=None, timestamp=False): + """Logs a client information alert to the console and the log file.\n`nolog=True` option skips logging to file.""" current_time = datetime.datetime.now().strftime("%H:%M:%S") - print(f'[{current_time}/INFO] {text}') + if module is not None: + if not timestamp: print(f'[{module}] {text}') + else: print(f'[{current_time}] [{module}] {text}') + else: + if not timestamp: print(text) + else: print(f'[{current_time}] {text}') if nolog == True: pass else: with open(log_path, 'a') as f: From b17b9d875f8d4b555c81a7ab5613f7e8f2c1199f Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:03:59 +0530 Subject: [PATCH 4/8] Improve logging system for `logger.warn()` --- utils/logger.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/utils/logger.py b/utils/logger.py index 306f0072..c2016555 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -25,13 +25,17 @@ def info(text:str, *, nolog=False, module=None, timestamp=False): with open(log_path, 'a') as f: f.write(f'[{current_time}/INFO] {text}\n') f.close() -def warn(text:str, *, nolog=False): + +def warn(text:str, *, nolog=False, module=None): + """Logs a client warning to the console and the log file.\n`nolog=True` option skips logging to file.""" current_time = datetime.datetime.now().strftime("%H:%M:%S") - print(f'[{current_time}/WARN] {text}') + if module is not None: print(f'{colours.warning}[{module}] WARN: {text}{colours.end}') + else: print(f'{colours.warning}WARN: {text}{colours.end}') if nolog == True: pass else: with open(log_path, 'a') as f: - f.write(f'[{current_time}/WARN] {text}\n') + if module is not None: f.write(f'[{current_time}] [{module}] WARN: {text}\n') + else: f.write(f'[{current_time}] WARN: {text}\n') f.close() def error(text:str, *, nolog=False): current_time = datetime.datetime.now().strftime("%H:%M:%S") From ca79b37d9408d5f0b05e1de8111f971664f75c63 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:04:16 +0530 Subject: [PATCH 5/8] Improve logging system for `logger.error()` --- utils/logger.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/utils/logger.py b/utils/logger.py index c2016555..f2ab518b 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -37,11 +37,15 @@ def warn(text:str, *, nolog=False, module=None): if module is not None: f.write(f'[{current_time}] [{module}] WARN: {text}\n') else: f.write(f'[{current_time}] WARN: {text}\n') f.close() -def error(text:str, *, nolog=False): + +def error(text:str, *, nolog=False, module=None): + """Logs an error to the console and the errors log file.\n`nolog=True` option skips logging to file.""" current_time = datetime.datetime.now().strftime("%H:%M:%S") - print(f'{colours.error}[{current_time}/ERROR] {text}{colours.end}') + if module is not None: print(f'{colours.error}[{module}] ERROR: {text}{colours.end}') + else: print(f'{colours.error}ERROR: {text}{colours.end}') if nolog == True: pass else: with open(error_path, 'a') as f: - f.write(f'[{current_time}/ERROR] {text}\n') + if module is not None: f.write(f'[{current_time}] [{module}] ERROR: {text}\n') + else: f.write(f'[{current_time}] ERROR: {text}\n') f.close() From 19967f94a2affd3aa115038ff640392fb4024041 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:04:48 +0530 Subject: [PATCH 6/8] Add log file support for `logger.info()` --- utils/logger.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/logger.py b/utils/logger.py index f2ab518b..fdb0c4ae 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -23,7 +23,8 @@ def info(text:str, *, nolog=False, module=None, timestamp=False): if nolog == True: pass else: with open(log_path, 'a') as f: - f.write(f'[{current_time}/INFO] {text}\n') + if module is not None: f.write(f'[{current_time}] [{module}] {text}\n') + else: f.write(f'[{current_time}] {text}\n') f.close() def warn(text:str, *, nolog=False, module=None): From 918c3a435f650557b9c7422f5143f6f2db1c7315 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:05:05 +0530 Subject: [PATCH 7/8] Beautify the code a little bit --- utils/logger.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/logger.py b/utils/logger.py index fdb0c4ae..d4d2435f 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -2,6 +2,7 @@ import time import datetime +# Variables and Classes log_path = "logs/info-log.txt" error_path = "logs/error-log.txt" class colours: From 5557262c6f5be5deb0799b89dc9752fa69bda070 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:05:50 +0530 Subject: [PATCH 8/8] Add new class `logger.StartupLog()` for logging client startup logs --- utils/logger.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/utils/logger.py b/utils/logger.py index d4d2435f..9f337d91 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -51,3 +51,34 @@ def error(text:str, *, nolog=False, module=None): if module is not None: f.write(f'[{current_time}] [{module}] ERROR: {text}\n') else: f.write(f'[{current_time}] ERROR: {text}\n') f.close() + +# Startup Log Class +class StartupLog(): + def __init__(self, log_path: str, *, add_timestamps: bool = False, clear_old_logs: True): + """Used to log the isobot client's startup logs.""" + self.log_path = log_path + self.add_timestamps = add_timestamps + if clear_old_logs: + with open(log_path, 'w+', encoding="utf-8") as f: + f.write("# Startup log will be shown here!\n") + f.close() + + def clean(self, data: str): + """Cleans out any extra metadata (like colour codes) from the data.""" + color_codes = ['\033[91m', '\033[92m', '\033[93m', '\033[33m', '\033[0m'] + data_d = data # Specify new isolated variable for performing string operations on + for x in color_codes: + if x in data_d: + data_d = data_d.replace(x, "") + return data_d + + def log(self, data: str, *, print_log: bool = True): + """Writes a log to the specified log file.""" + if print_log: print(data) + data = self.clean(data) + with open(self.log_path, 'a', encoding="utf-8") as f: + if self.add_timestamps: + current_time = datetime.datetime.now().strftime("%H:%M:%S") + f.write(f"[{current_time}] {data}\n") + else: f.write(f"{data}\n") + f.close()