import threading
from datetime import datetime
import time

class Logger:
    _instance = None
    _lock = threading.Lock()

    def __new__(cls):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    time.sleep(0.01)
                    cls._instance = super(Logger, cls).__new__(cls)
                    cls._instance._initialize()
        return cls._instance
    
    def _initialize(self):
        self.log_file = open("test.log", "a")
    
    def log(self, message):
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.log_file.write(f"[{timestamp}] {message}\n")

def create_instance(thread_id):
    logger = Logger()
    logger.log(f"Message #{thread_id}")
    

if __name__ == "__main__":
    threads = [threading.Thread(target=create_instance, args=(i,)) for i in range(10)]

    for t in threads:
        t.start()
        
    # time.sleep(3)

    for t in threads:
        t.join()

    print("Готово. Все потоки записали лог.")
