@@ -25,8 +25,11 @@ def __init__(self):
2525 self ._file = None
2626 self ._enabled = False
2727 self ._path = ""
28+ self ._timestamp = True
2829
29- def start (self , path : str ) -> tuple [bool , str ]:
30+ def start (
31+ self , path : str , append : bool = True , timestamp : bool = True
32+ ) -> tuple [bool , str ]:
3033 """Start recording logs to file."""
3134 with self ._lock :
3235 if self ._enabled :
@@ -41,11 +44,15 @@ def start(self, path: str) -> tuple[bool, str]:
4144 if dir_path and not os .path .exists (dir_path ):
4245 os .makedirs (dir_path , exist_ok = True )
4346
44- self ._file = open (path , "a" , encoding = "utf-8" )
47+ mode = "a" if append else "w"
48+ self ._file = open (path , mode , encoding = "utf-8" )
4549 self ._enabled = True
4650 self ._path = path
51+ self ._timestamp = timestamp
4752
48- logger .info (f"Log recording started: { path } " )
53+ logger .info (
54+ f"Log recording started: { path } (append={ append } , timestamp={ timestamp } )"
55+ )
4956 return True , ""
5057 except Exception as e :
5158 self ._enabled = False
@@ -83,14 +90,16 @@ def write(self, message: str):
8390 return
8491
8592 try :
86- timestamp = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S.%f" )[:- 3 ]
8793 # Handle multi-line messages
8894 lines = message .split ("\n " )
8995 for i , line in enumerate (lines ):
9096 if (
9197 line or i == 0
9298 ): # Write first line even if empty, skip other empty lines
93- if i == 0 :
99+ if i == 0 and self ._timestamp :
100+ timestamp = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S.%f" )[
101+ :- 3
102+ ]
94103 self ._file .write (f"[{ timestamp } ] { line } \n " )
95104 else :
96105 self ._file .write (f"{ line } \n " )
0 commit comments