First, PDB use
The PDB is a Python-brought package that provides an interactive source code debugging feature for Python programs, including setting breakpoints, stepping through debugging, entering function debugging, viewing current code, viewing stack fragments, dynamically changing the values of variables, and more.
Insert a program in the middle of the program (import PDB Pdb.set_trace ()), relative to the general IDE inside the breakpoint and then start Debug, but this way is hardcode
1. Add Breakpoint
#!/usr/bin/pythonImportpdb _DEBUG=TruedefDebug_demo (val): if _debug = = True: pdb.set_trace () ifVal <= 1600 : Print "Level 1" Print0elifVal <= 3500 : Print "Level 2" Print(val-1600) * 0.05elifVal <= 6500 : Print "Level 3" Print(val-3500) * 0.10 + (3500-1600) * 0.05Else: Print "Level 4" Print(val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05#~def Debug_demoif __name__=="__main__": Debug_demo (4500)
2. Start Commissioning
Second, the use of logging
#Encoding:utf-8ImportLogging Log1= Logging.getlogger ('A.B.C') log2= Logging.getlogger ('A.D.E') Filehandler= Logging. Filehandler ('Test.log','a') Formatter= Logging. Formatter ('% (name) s% (asctime) s% (levelname) s% (message) s') Filehandler.setformatter (formatter) filter= Logging. Filter ('a') Filehandler.addfilter (filter) Log1.addhandler (Filehandler) Log2.addhandler (Filehandler) Log1.setlevel ( Logging. DEBUG) Log2.setlevel (logging. DEBUG) Log1.debug ('It's a debug info for Log1') Log1.info ('normal infor for Log1') log1.warning ('Warning Info for LOG1:B.C') Log1.error ('Error info for LOG1:ABCD') log1.critical ('Critical info for Log1:not worked') Log2.debug ('Debug Info for log2') Log2.info ('Normal info for log2') log2.warning ('Warning Info for log2') Log2.error ('ERROR:B.C') log2.critical ('Critical')
Logging Lib consists of 4 main objects
- Logger:logger is the interface for program information output. It is scattered in different code so that the program can log the appropriate information at run time and determine what information needs to be output and distribute the information to its associated handler, based on the logging level or filter set. Common methods are Logger.setlevel (), Logger.addhandler (), Logger.removehandler (), Logger.addfilter (), Logger.debug (), Logger.info (), logger.warning (), Logger.error (), GetLogger (), etc. Logger supports hierarchical inheritance relationships, and the name of a child logger is usually the way the parent logger.name. If you do not create an instance of logger, use the default root logger to get the root logger instance by Logging.getlogger () or Logging.getlogger ("").
- Handler:handler is used to process the output of information, which can be output to a console, file, or network. You can add handler to the Logger object through Logger.addhandler (), and the commonly used handler have Streamhandler and Filehandler classes. The Streamhandler sends an error message to the stream, and the Filehandler class is used to output the log information to the file, which is defined by the two handler in the Logging core module. Other hander are defined in the Logging.handles module, such as Httphandler,sockethandler.
- Formatter:formatter determines the format of the log information, which is defined using the form similar to% (< dictionary key >) s, such as '% (asctime) s-% (levelname) s-% (message ) s ', supported keys can be viewed in Python's own document LogRecord attributes.
- Filter:filter is used to determine what information needs to be output. Can be used by handler and logger, support hierarchical relations, such as if the filter is set to a name called A.B logger, then the logger and its sub-logger information will be output, such as A.B,A.B.C.