oop/2026-03-31/logger.py (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from datetime import datetime
class Logger:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(Logger, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
if not hasattr(self, "initialized"):
self.initialized = True
self.__file = open("log.txt", "a")
print("Singleton-Instanz wurde erstellt.")
@staticmethod
def getInstance():
if Logger._instance is None:
Logger._instance = Logger()
return Logger._instance
def log(self, logstring):
self.__file.write(f"{str(datetime.today())}: {logstring}\n")
|