简体   繁体   English

用Python记录事件; 如何在类内记录事件?

[英]Logging events in Python; How to log events inside classes?

I built (just for fun) 3 classes to help me log some events in my work. 我建立了(只是为了好玩)3个类来帮助我记录工作中的一些事件。

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. 想象一下,我还有其他类,例如Employee,并且我想记录该类内部发生的事件。

How can i do that without always passing a logHandler instance to every other class i want to log? 我如何做到这一点而不必始终将logHandler实例传递给我要记录的其他所有类? My idea would be to pass a logHandler to every init function, and then use it inside it. 我的想法是将logHandler传递给每个init函数,然后在其中使用它。

How can that be done, without doing what i specified? 不执行我指定的操作怎么办?

How would it work with global logHandler? 它将如何与全局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? 有没有一种方法可以在运行时发现程序中是否有logHandler实例,并使用它来创建消息?

Thanks 谢谢

Just create an instance of your classes in the module you posted. 只需在发布的模块中创建类的实例。 Then just import your logging module in every file you want to log from and do something like this: 然后,只需将日志记录模块导入要从中进行记录的每个文件中,然后执行以下操作:

yourloggingmodule.handler.newLogMessage(...)

Where handler is the name of the instance you created. 其中handler是您创建的实例的名称。

You could use the Borg pattern , meaning you can create local instances of your logger object and yet have them access the same state. 您可以使用Borg模式 ,这意味着您可以创建logger对象的本地实例,并使它们访问相同的状态。 Some would say that is elegant, others may say it's confusing. 有些人会说这很优雅,其他人可能会说这很混乱。 You decide. 你决定。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM