Added timestamp to logging

This commit is contained in:
Szirtesi,Tamás Gábor 2023-10-09 16:06:05 +02:00
parent 1b283cf2a8
commit 838821bee5

View File

@ -4,17 +4,20 @@ import threading
import time import time
import warnings import warnings
from typing import Callable, Union from typing import Callable, Union
import datetime
from hc_spider.model import SharedObjects from hc_spider.model import SharedObjects
class JSONLogger(threading.Thread): class JSONLogger(threading.Thread):
logging_interval: int _logging_interval: int
_timestamp: str
_shared_objects: SharedObjects _shared_objects: SharedObjects
def __init__(self, **kwargs) -> None: def __init__(self, **kwargs) -> None:
self._shared_objects = SharedObjects(**kwargs) self._shared_objects = SharedObjects(**kwargs)
self.logging_interval = self._shared_objects.config.get("logging_interval") self._logging_interval = self._shared_objects.config.get("logging_interval")
self._timestamp = f"{datetime.datetime.now():%Y%m%d_%H%M%S}"
super().__init__() super().__init__()
self.daemon = True self.daemon = True
self.name = "JSONLogger" self.name = "JSONLogger"
@ -28,7 +31,7 @@ class JSONLogger(threading.Thread):
print(f"[{self.name}] is starting") print(f"[{self.name}] is starting")
while self._shared_objects.shutdown_event.is_set() is False: while self._shared_objects.shutdown_event.is_set() is False:
time.sleep(self.logging_interval) time.sleep(self._logging_interval)
try: try:
self._dump_logs() self._dump_logs()
@ -58,7 +61,7 @@ class JSONLogger(threading.Thread):
def _dump_data_to_file(self, data: list | dict, filename: str, sort_key: Union[Callable, None]) -> None: def _dump_data_to_file(self, data: list | dict, filename: str, sort_key: Union[Callable, None]) -> None:
if self._shared_objects.shutdown_event.is_set() is True: if self._shared_objects.shutdown_event.is_set() is True:
data.sort(key=sort_key) data.sort(key=sort_key)
with open(f"{self._shared_objects.config.get('log_dir')}/{filename}.json", "w") as f: with open(f"{self._shared_objects.config.get('log_dir')}/{filename}_{self._timestamp}.json", "w") as f:
f.write(json.dumps(data, indent=4)) f.write(json.dumps(data, indent=4))
def _dump_not_valid_urls(self) -> None: def _dump_not_valid_urls(self) -> None: