@@ -26,25 +26,31 @@ class Logger:
26
26
Args:
27
27
log_path (str): The path to the log file directory
28
28
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])
29
30
"""
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]" ):
31
32
self .log_path = log_path
32
33
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 ()
34
36
35
37
atexit .register (self ._exit_handler )
36
38
37
- def _getLogDateTime (self ):
39
+ def _getFormattedTimeStamp (self ):
38
40
now = datetime .now ()
39
41
return now .strftime (self .log_format )
40
42
43
+ def _getInlineFormattedTimeStamp (self ):
44
+ now = datetime .now ()
45
+ return now .strftime (self .log_inline_format )
46
+
41
47
def _writeToLogFile (self , txt ):
42
48
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 :
44
50
log_file .write (txt )
45
51
log_file .close ()
46
52
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 )
48
54
49
55
def _exit_handler (self ):
50
56
exc_type , exc_value , exc_traceback = sys .exc_info ()
@@ -64,7 +70,7 @@ def warning(self, *txt):
64
70
for substr in txt :
65
71
string += str (substr ) + " "
66
72
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 " )
68
74
69
75
def error (self , * txt ):
70
76
""" Logs an error message
@@ -76,7 +82,7 @@ def error(self, *txt):
76
82
for substr in txt :
77
83
string += str (substr ) + " "
78
84
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 " )
80
86
81
87
def debug (self , * txt ):
82
88
""" Logs a debug message
@@ -88,7 +94,7 @@ def debug(self, *txt):
88
94
for substr in txt :
89
95
string += str (substr ) + " "
90
96
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 " )
92
98
93
99
def success (self , * txt ):
94
100
""" Logs a success message
@@ -100,7 +106,7 @@ def success(self, *txt):
100
106
for substr in txt :
101
107
string += str (substr ) + " "
102
108
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 " )
104
110
105
111
def info (self , * txt ):
106
112
""" Logs a success message
@@ -112,5 +118,5 @@ def info(self, *txt):
112
118
for substr in txt :
113
119
string += str (substr ) + " "
114
120
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 " )
116
122
0 commit comments