0
点赞
收藏
分享

微信扫一扫

Python中打印日志,文件,行号,当前时间

mm_tang 2022-03-25 阅读 76

写一个函数实现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>

举报

相关推荐

0 条评论