写一个函数实现python的日志功能:
调用函数,打印当前时间,打印当前行号,文件函数名称
便于找bug
import sys,time
def Log(msg,line,name):
#文件地址 __file__,可选添加
date = time.strftime('%Y.%m.%d %H:%M:%S ',time.localtime(time.time()))
print(date+':'+ msg +', Line '+line+' , in '+name)
if __name__ == '__main__':
Log('hello',str(sys._getframe().f_lineno),sys._getframe().f_code.co_name)
#2022.03.25 11:52:15 :hello, Line 9 , in <module>
打印结果:2022.03.25 11:52:15 :hello, Line 9 , in <module>