我们如何在 python 中记录大型管道中每个模块花费的时间



我有一个python管道代码,它调用了大约10到12个python类。 我想知道并记录每个类中每个方法所消耗的时间。 谁能建议一些在python中做到这一点的方法 我正在使用python内置的日志记录模块进行日志记录

import logging
import time
class myClass():
logErrors = False
logger = None
def log(self, msg):
if self.logger != None:
self.logger.warning(msg)
def __init__(self, log=False):
self.logErrors = log
if self.logErrors == True:
self.logger = logging.getLogger("myClass")
self.logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.ch = logging.StreamHandler()
self.ch.setLevel(logging.INFO)
self.ch.setFormatter(formatter)
self.logger.addHandler(self.ch)

def method1(self):
# log start time of method
self.log("method1() started")
# code
print("foobar")
time.sleep(1)
# log end time of method
self.log("method1() ended")
my = myClass(log=True)
my.method1()

最新更新