This article mainly describes the Python record detailed call stack log method, involving Python call stack log related techniques, with a certain reference value, the need for friends can refer to the following
The example in this article describes how Python records verbose call stack logs. Share to everyone for your reference. The implementation method is as follows:
Import Sysimport osdef Detailtrace (info): retstr = "" curindex=0 f = sys._getframe () f = f.f_back # First frame is detailtrace, ignore it while Hasattr (f, "F_code"): co = f.f_code retstr = "%s (%s:%s)"% (OS . Path.basename (Co.co_filename), co.co_name, F.f_lineno) + retstr f = f.f_back print retstr+ Infodef foo (): detailtrace ("Hello World") def Bar (): foo () def main (): bar () if name = = "Main": Main ()
Output:
aaa1.py (<module>:27)->aaa1.py (main:24)->aaa1.py (bar:21)->aaa1.py (foo:18)->hello World