python获取当前位置所在的行号和函数名

最近给函数打log时,想指出加入Log的地方,包括时间、文件名、函数名、行号,这样以后找起来会比较容易。通过设这logging的fomatter可以实现,但每次都做太费劲了,于是找了个得到这些信息的方法,也是使用了logging里面的做法,通过异常得到执行信息。

1 def get_head_info():
2     try:
3         raise Exception
4     except:
5         f = sys.exc_info()[2].tb_frame.f_back
6     return '%s, %s, %s, %s, ' % (str(datetime.now()), f.f_code.co_filename, f.f_code.co_name, str(f.f_lineno))
原文地址:https://www.cnblogs.com/njucslzh/p/2688932.html