Hello guys.
I built (just for fun) 3 classes to help me log some events in my work.
here are them:
class logMessage:
def __init__(self,objectName,message,messageType):
self.objectName = objectName
self.message = message
self.messageType = messageType
self.dateTime = datetime.datetime.now()
def __str__(self):
return str(self.dateTime) + "\nObjeto de valor " + str(self.objectName) + " gerou uma mensagem do tipo: " + self.messageType + "\n" + self.message + "\n"
class logHandler():
def __init__(self):
self.messages = []
def __getitem__(self,index):
return self.messages[index]
def __len__(self):
return len(self.messages)
def __str__(self):
colecaoString = ""
for x in self.messages:
colecaoString += str(x) + "\n"
return colecaoString
def dumpItem(self,index):
temp = self.messages[index]
del self.messages[index]
return str(temp)
def append(self,log):
if isinstance(log,logMessage.logMessage):
self.messages.append(log)
else:
self.newLogMessage(log, "Wrong object type. Not a log message. Impossible to log.","Error")
def newLogMessage(self,objectInstance,message,messageType):
newMessage = logMessage.logMessage(objectInstance,message,messageType)
self.append(newMessage)
Here is my question:
Imagine i have other classes, such as Employee, and i want to log an event that happened INSIDE that class.
How can i do that without always passing a logHandler instance to every other class i want to log? My idea would be to pass a logHandler to every init function, and then use it inside it.
How can that be done, without doing what i specified?
How would it work with global logHandler? Is there a way to discover in runtime if there is a logHandler instance in the program, and use it to create the messages?
Thanks