Tag: Pat time value Dice ack localtime ALT requires for
Recently wrote a program that can get a list of folders and files updated by the specified folder within a set time, and do something based on the list of updates obtained. Because the program is put on the server to run, in order to ensure that the program in the process of running, from time to time to jump out of some unusual information to scare other users, in the program added exception handling. Organize the information on the web and try to use Sys.exce_info () and Traceback together. The effect is good, the following information is the way I am currently handling exceptions, where type is the type of exception, value is the cause of the exception, Traceback is the exception information through the Traceback Chase, to locate the code that caused the exception.
2016-11-07 22:07:56-------------------------------'exceptions. TypeError'> not strtraceback: [(' filename ' ) ' function name ' , ' the line of code where the exception occurred ')]
In Try...except, the following two lines are used to record the exception condition. Depending on how the program continues after an exception, you need to look at the specific requirements.
TP,VAL,TD = sys.exc_info () log.logerexception (TP,VAL,TD)
The specific code is as follows
1 ImportOS2 Import Time3 ImportTraceback4 ImportSYS5 6 deflogerexception (TP,VAL,TD):7EType =STR (TP)8Evalue =Str (val)9ETB =TRACEBACK.EXTRACT_TB (TD)TenErrorMsg ="Type:"+ EType +"\ n" OneErrorMsg + ="Value:"+ Evalue +"\ n" AErrorMsg + ="Traceback:"+ STR (ETB) +"\ n" - WriteToFile (errormsg) - the defWriteToFile (errormsg): -LogFilePath = Os.path.abspath ('.') +"/log" - if notos.path.exists (LogFilePath): - Os.mkdir (LogFilePath) + -LogFile = Time.strftime ("%y%m%d", Time.localtime ()) +". txt" +fp = open (LogFilePath +"/"+ LogFile,"a") Aisotimeformat="%y-%m-%d%x" atHappeningtime =time.strftime (Isotimeformat, Time.localtime ()) -Usermsg ="" -Usermsg + = Happeningtime +"\ n-------------------------------\ n" -Usermsg + =errormsg -Fp.write (usermsg +"\ n") -Fp.close ()
View Code
[Python]python, handling exception information using Traceback