GoldyBot.logging

  1from __future__ import annotations
  2import os
  3import time
  4from datetime import date
  5import traceback
  6
  7import GoldyBot
  8
  9MODULE_NAME = "LOGGING"
 10
 11def print_and_log(importance_level=None, text=None, no_traceback=False):
 12    """Goldy Bot's Print statement. PLEASE use this statement instead of the usual 'print()'."""
 13    if text == None: # Just makes a new line.
 14        if importance_level == None:
 15            print("")
 16            path_to_log = write_to_log("")
 17            return path_to_log
 18
 19        if isinstance(importance_level, GoldyBot.errors.GoldyBotError):
 20            time = get_time_and_date("time")
 21            context = ("({}) [ERROR] {}".format(time, text))
 22            print(f"\u001b[31m{context} \n{traceback.format_exc()}\u001b[0m") #Red
 23            write_to_log(f"{context}\n\n{traceback.format_exc()}")
 24            return
 25
 26        else:
 27            time = get_time_and_date("time")
 28            context = ("({}) {}".format(time, importance_level))
 29
 30            print(f"\u001b[37m{context}\u001b[0m")
 31            
 32            path_to_log = write_to_log(context)
 33            return path_to_log
 34
 35    if not text == None:
 36        if importance_level == None:
 37            time = get_time_and_date("time")
 38            context = ("({}) {}".format(time, text))
 39
 40            print(f"\u001b[37m{context}\u001b[0m")
 41            write_to_log(context)
 42            return
 43
 44        if importance_level.upper() == 'INFO':
 45            time = get_time_and_date("time")
 46            context = ("({}) [INFO] {}".format(time, text))
 47
 48            print(f"\u001b[36m{context}\u001b[0m") # Blue
 49            write_to_log(context)
 50            return
 51
 52        if importance_level.upper() == 'INFO_2':
 53            time = get_time_and_date("time")
 54            context = ("({}) [INFO] {}".format(time, text))
 55
 56            print(f"\u001b[32m{context}\u001b[0m") # Green
 57            write_to_log(context)
 58            return
 59
 60        if importance_level.upper() == 'INFO_3':
 61            time = get_time_and_date("time")
 62            context = ("({}) [INFO] {}".format(time, text))
 63
 64            print(f"\u001b[38;5;51m{context}\u001b[0m") # Clay
 65            write_to_log(context)
 66            return
 67
 68        if importance_level.upper() == 'INFO_4':
 69            time = get_time_and_date("time")
 70            context = ("({}) [INFO] {}".format(time, text))
 71
 72            print(f"\u001b[38;5;200m{context}\u001b[0m") # Purple
 73            write_to_log(context)
 74            return
 75
 76        if importance_level.upper() == 'INFO_5':
 77            time = get_time_and_date("time")
 78            context = ("({}) [INFO] {}".format(time, text))
 79
 80            print(f"\u001b[38;5;139m{context}\u001b[0m") # Pink Grey
 81            write_to_log(context)
 82            return
 83
 84        if importance_level.upper() == 'WARN':
 85            time = get_time_and_date("time")
 86            context = ("({}) [WARN] {}".format(time, text))
 87            
 88            if not no_traceback:
 89                print(f"\u001b[33m{context}\u001b[0m") #Yellow
 90            write_to_log(f"{context}\n\n{traceback.format_exc()}")
 91            return
 92
 93
 94        if importance_level.upper() == 'ERROR':
 95            time = get_time_and_date("time")
 96            context = ("({}) [ERROR] {}".format(time, text))
 97            if not no_traceback:
 98                print(f"\u001b[31m{context} \n{traceback.format_exc()}\u001b[0m") #Red
 99            write_to_log(f"{context}\n\n{traceback.format_exc()}")
100            return
101
102        if importance_level.upper() == 'APP_NAME':
103            context = (text)
104
105            print(f"\u001b[36m{context}\u001b[0m")
106            write_to_log(context)
107            return
108
109log = print_and_log
110
111# Date and time generator function.
112def get_time_and_date(option):
113
114    if option.lower() == 'date':
115        today = date.today()
116        # dd/mm/YY
117        current_date = today.strftime("%d-%m-%Y")
118        return current_date
119
120    if option.lower() == 'time':
121        t = time.localtime()
122        # hh/mm/ss
123        current_time = time.strftime("%H.%M.%S", t)
124        return current_time
125
126    if option.lower() == 'both':
127        #Date
128        today = date.today()
129        current_date = today.strftime("%d-%m-%Y")
130
131        #Time
132        t = time.localtime()
133        current_time = time.strftime("%H.%M.%S", t)
134
135        both = "({}) [{}]".format(current_date, current_time)
136        return both
137
138log_date_and_time = get_time_and_date("both") #Time jsqp was started.
139
140# Txt Logging Function
141def write_to_log(text):
142    #Create Logs Folder.
143    paths = GoldyBot.paths
144
145    try:
146        os.mkdir(paths.LOGS)
147    except FileExistsError as e:
148        pass
149
150    except Exception:
151        print("^ Could not write_to_log that ^")
152        return False
153
154    f = open(f"{paths.LOGS}/{log_date_and_time}.txt", "a", encoding='utf-8')
155    f.write(text + "\n")
156
157    return f"{paths.LOGS}/{log_date_and_time}.txt"
def log(importance_level=None, text=None, no_traceback=False):
 12def print_and_log(importance_level=None, text=None, no_traceback=False):
 13    """Goldy Bot's Print statement. PLEASE use this statement instead of the usual 'print()'."""
 14    if text == None: # Just makes a new line.
 15        if importance_level == None:
 16            print("")
 17            path_to_log = write_to_log("")
 18            return path_to_log
 19
 20        if isinstance(importance_level, GoldyBot.errors.GoldyBotError):
 21            time = get_time_and_date("time")
 22            context = ("({}) [ERROR] {}".format(time, text))
 23            print(f"\u001b[31m{context} \n{traceback.format_exc()}\u001b[0m") #Red
 24            write_to_log(f"{context}\n\n{traceback.format_exc()}")
 25            return
 26
 27        else:
 28            time = get_time_and_date("time")
 29            context = ("({}) {}".format(time, importance_level))
 30
 31            print(f"\u001b[37m{context}\u001b[0m")
 32            
 33            path_to_log = write_to_log(context)
 34            return path_to_log
 35
 36    if not text == None:
 37        if importance_level == None:
 38            time = get_time_and_date("time")
 39            context = ("({}) {}".format(time, text))
 40
 41            print(f"\u001b[37m{context}\u001b[0m")
 42            write_to_log(context)
 43            return
 44
 45        if importance_level.upper() == 'INFO':
 46            time = get_time_and_date("time")
 47            context = ("({}) [INFO] {}".format(time, text))
 48
 49            print(f"\u001b[36m{context}\u001b[0m") # Blue
 50            write_to_log(context)
 51            return
 52
 53        if importance_level.upper() == 'INFO_2':
 54            time = get_time_and_date("time")
 55            context = ("({}) [INFO] {}".format(time, text))
 56
 57            print(f"\u001b[32m{context}\u001b[0m") # Green
 58            write_to_log(context)
 59            return
 60
 61        if importance_level.upper() == 'INFO_3':
 62            time = get_time_and_date("time")
 63            context = ("({}) [INFO] {}".format(time, text))
 64
 65            print(f"\u001b[38;5;51m{context}\u001b[0m") # Clay
 66            write_to_log(context)
 67            return
 68
 69        if importance_level.upper() == 'INFO_4':
 70            time = get_time_and_date("time")
 71            context = ("({}) [INFO] {}".format(time, text))
 72
 73            print(f"\u001b[38;5;200m{context}\u001b[0m") # Purple
 74            write_to_log(context)
 75            return
 76
 77        if importance_level.upper() == 'INFO_5':
 78            time = get_time_and_date("time")
 79            context = ("({}) [INFO] {}".format(time, text))
 80
 81            print(f"\u001b[38;5;139m{context}\u001b[0m") # Pink Grey
 82            write_to_log(context)
 83            return
 84
 85        if importance_level.upper() == 'WARN':
 86            time = get_time_and_date("time")
 87            context = ("({}) [WARN] {}".format(time, text))
 88            
 89            if not no_traceback:
 90                print(f"\u001b[33m{context}\u001b[0m") #Yellow
 91            write_to_log(f"{context}\n\n{traceback.format_exc()}")
 92            return
 93
 94
 95        if importance_level.upper() == 'ERROR':
 96            time = get_time_and_date("time")
 97            context = ("({}) [ERROR] {}".format(time, text))
 98            if not no_traceback:
 99                print(f"\u001b[31m{context} \n{traceback.format_exc()}\u001b[0m") #Red
100            write_to_log(f"{context}\n\n{traceback.format_exc()}")
101            return
102
103        if importance_level.upper() == 'APP_NAME':
104            context = (text)
105
106            print(f"\u001b[36m{context}\u001b[0m")
107            write_to_log(context)
108            return

Goldy Bot's Print statement. PLEASE use this statement instead of the usual 'print()'.

def get_time_and_date(option):
113def get_time_and_date(option):
114
115    if option.lower() == 'date':
116        today = date.today()
117        # dd/mm/YY
118        current_date = today.strftime("%d-%m-%Y")
119        return current_date
120
121    if option.lower() == 'time':
122        t = time.localtime()
123        # hh/mm/ss
124        current_time = time.strftime("%H.%M.%S", t)
125        return current_time
126
127    if option.lower() == 'both':
128        #Date
129        today = date.today()
130        current_date = today.strftime("%d-%m-%Y")
131
132        #Time
133        t = time.localtime()
134        current_time = time.strftime("%H.%M.%S", t)
135
136        both = "({}) [{}]".format(current_date, current_time)
137        return both
def write_to_log(text):
142def write_to_log(text):
143    #Create Logs Folder.
144    paths = GoldyBot.paths
145
146    try:
147        os.mkdir(paths.LOGS)
148    except FileExistsError as e:
149        pass
150
151    except Exception:
152        print("^ Could not write_to_log that ^")
153        return False
154
155    f = open(f"{paths.LOGS}/{log_date_and_time}.txt", "a", encoding='utf-8')
156    f.write(text + "\n")
157
158    return f"{paths.LOGS}/{log_date_and_time}.txt"