Example

Get variables

importsysimporttracebackfromStringIOimportStringIOdefprint_exc(tb=None):""" Print the usual traceback information, followed by a listing of all the local variables in each frame. :rtype StringIO.StringIO: all the traceback information """output=StringIO()iftb==None:printsys.exc_info()tb=sys.exc_info()[2]printtbwhile1:ifnottb.tb_next:breaktb=tb.tb_nextstack=[]f=tb.tb_framewhilef:stack.append(f)f=f.f_backstack.reverse()output.write("Locals by frame, innermost last\n")forframeinstack:output.write("Frame %s in %s at line %s\n"%(frame.f_code.co_name,frame.f_code.co_filename,frame.f_lineno))forkey,valueinframe.f_locals.items():output.write("\t%20s = "%key)#We have to be careful not to cause a new error in our error#printer! Calling str() on an unknown object could cause an#error we don't want.try:output.write("%s\n"%value)except:output.write("<ERROR WHILE PRINTING VALUE>\n"):::pythonimportloggingclassSMSHandler(logging.Handler):# Inherit from logging.Handlerdefemit(self,record):printrecordimportpdbpdb.set_trace()importlogging.handlers# Create a logging object (after configuring logging)logging.basicConfig(filename="as",level=logging.WARNING)logger=logging.getLogger()# A little trickery because, at least for me, directly creating# an SMSHandler object didn't worklogging.handlers.SMSHandler=SMSHandler# create the handler objecttestHandler=logging.handlers.SMSHandler()# Configure the handler to only send SMS for critical errorstestHandler.setLevel(logging.CRITICAL)# and finally we add the handler to the logging objectlogger.addHandler(testHandler)# And finally a testlogger.debug('Test 1')logger.info('Test 2')logger.warning('Test 3')logger.error('Test 4')logger.critical('Test 5')