import sys
def Log(msg):
print('Print Message: '+msg+' ,File: "'+__file__+'", Line '+str(sys._getframe().f_lineno)+' , in '+sys._getframe().f_code.co_name)
if __name__ == '__main__':
Log('hello') # Print Message: hello ,File: "i.py", Line 4 , in Log
#Python学习交流QQ群:778463939寻找有志同道合的小伙伴
The Log function is a re-encapsulation of print, which can display the script name, line number and function name of the error program.
among them,
- sys._getframe().f_lineno: current line number, int type
- sys._getframe().f_code.co_name: current file name string type
The function is executed as follows:
Print Message: hello ,File: "i.py", Line 4 , in Log