最近給函數打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))
