Skip to content

Commit 051618c

Browse files
0.3.1 - Total Utilities 12 - More log functionality and add .log to log files FINALLY
1 parent c3d2ffb commit 051618c

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea
22
.env
3+
logs
34
dist
45
build
56
rites/test.py

rites/logger.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,31 @@ class Logger:
2626
Args:
2727
log_path (str): The path to the log file directory
2828
log_format (str): The format of the log file name - default (log-%Y-%m-%d-%Hh-%Mm-%Ss)
29+
log_inline_format (str): The format for the time stamp on the log lines - default ([%Y-%m-%d %H:%M:%S])
2930
"""
30-
def __init__(self, log_path, log_format="log-%Y-%m-%d-%Hh-%Mm-%Ss"):
31+
def __init__(self, log_path, log_format="log-%Y-%m-%d-%Hh-%Mm-%Ss", log_inline_format="[%Y-%m-%d %H:%M:%S]"):
3132
self.log_path = log_path
3233
self.log_format = log_format
33-
self.logger_creation_time = self._getLogDateTime()
34+
self.log_inline_format = log_inline_format
35+
self.logger_creation_time = self._getFormattedTimeStamp()
3436

3537
atexit.register(self._exit_handler)
3638

37-
def _getLogDateTime(self):
39+
def _getFormattedTimeStamp(self):
3840
now = datetime.now()
3941
return now.strftime(self.log_format)
4042

43+
def _getInlineFormattedTimeStamp(self):
44+
now = datetime.now()
45+
return now.strftime(self.log_inline_format)
46+
4147
def _writeToLogFile(self, txt):
4248
try:
43-
with open(f"{self.log_path}/{self.logger_creation_time}", "a+") as log_file:
49+
with open(f"{self.log_path}/{self.logger_creation_time}.log", "a+") as log_file:
4450
log_file.write(txt)
4551
log_file.close()
4652
except FileNotFoundError:
47-
open(f"{self.log_path}/{self.logger_creation_time}", "w").write(txt)
53+
open(f"{self.log_path}/{self.logger_creation_time}.log", "w").write(txt)
4854

4955
def _exit_handler(self):
5056
exc_type, exc_value, exc_traceback = sys.exc_info()
@@ -64,7 +70,7 @@ def warning(self, *txt):
6470
for substr in txt:
6571
string += str(substr) + " "
6672
print(f"{Misc.ConsoleColors.white}[{Misc.ConsoleColors.rst}{Misc.ConsoleColors.warning}warning{Misc.ConsoleColors.rst}{Misc.ConsoleColors.white}]{Misc.ConsoleColors.rst} " + string)
67-
self._writeToLogFile(f"[warning] {string}\n")
73+
self._writeToLogFile(f"{self._getInlineFormattedTimeStamp()} [warning] {string}\n")
6874

6975
def error(self, *txt):
7076
""" Logs an error message
@@ -76,7 +82,7 @@ def error(self, *txt):
7682
for substr in txt:
7783
string += str(substr) + " "
7884
print(f"{Misc.ConsoleColors.white}[{Misc.ConsoleColors.rst}{Misc.ConsoleColors.error}error{Misc.ConsoleColors.rst}{Misc.ConsoleColors.white}]{Misc.ConsoleColors.rst} " + string)
79-
self._writeToLogFile(f"[error] {string}\n")
85+
self._writeToLogFile(f"{self._getInlineFormattedTimeStamp()} [error] {string}\n")
8086

8187
def debug(self, *txt):
8288
""" Logs a debug message
@@ -88,7 +94,7 @@ def debug(self, *txt):
8894
for substr in txt:
8995
string += str(substr) + " "
9096
print(f"{Misc.ConsoleColors.white}[{Misc.ConsoleColors.rst}{Misc.ConsoleColors.debug}debug{Misc.ConsoleColors.rst}{Misc.ConsoleColors.white}]{Misc.ConsoleColors.rst} " + string)
91-
self._writeToLogFile(f"[debug] {string}\n")
97+
self._writeToLogFile(f"{self._getInlineFormattedTimeStamp()} [debug] {string}\n")
9298

9399
def success(self, *txt):
94100
""" Logs a success message
@@ -100,7 +106,7 @@ def success(self, *txt):
100106
for substr in txt:
101107
string += str(substr) + " "
102108
print(f"{Misc.ConsoleColors.white}[{Misc.ConsoleColors.rst}{Misc.ConsoleColors.success}success{Misc.ConsoleColors.rst}{Misc.ConsoleColors.white}]{Misc.ConsoleColors.rst} " + string)
103-
self._writeToLogFile(f"[success] {string}\n")
109+
self._writeToLogFile(f"{self._getInlineFormattedTimeStamp()} [success] {string}\n")
104110

105111
def info(self, *txt):
106112
""" Logs a success message
@@ -112,5 +118,5 @@ def info(self, *txt):
112118
for substr in txt:
113119
string += str(substr) + " "
114120
print(f"{Misc.ConsoleColors.white}[{Misc.ConsoleColors.rst}{Misc.ConsoleColors.info}info{Misc.ConsoleColors.rst}{Misc.ConsoleColors.white}]{Misc.ConsoleColors.rst} " + string)
115-
self._writeToLogFile(f"[info] {string}\n")
121+
self._writeToLogFile(f"{self._getInlineFormattedTimeStamp()} [info] {string}\n")
116122

0 commit comments

Comments
 (0)