O
Oliver Walczak
This seems to be a quite difficult approach. Try this:
#####################################################################
import traceback
class MyTraceback:
def __init__(self):
self.clear()
def clear(self):
self.s = ''
def write(self, s):
self.s += s
def read(self):
return self.s
def catch(self):
traceback.print_exc(None, self)
if __name__ == '__main__':
myTcb = MyTraceback()
try:
a = 1/0
except:
myTcb.clear()
myTcb.catch()
print myTcb.read()
######################################################################
Call clear() each time before you expect a new traceback output. You can
import this class and don't need to import traceback in your main project.
Oliver
I define the following module "Excepts.py" for logging exceptions in daemon
processes:
------------------------------------------
import sys,traceback
def error():
tb =
traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info(
)[2])
return tb[len(tb)-1].replace('\n','')
def errorstack():
return
''.join(traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.e
xc_info()[2]))
#####################################################################
import traceback
class MyTraceback:
def __init__(self):
self.clear()
def clear(self):
self.s = ''
def write(self, s):
self.s += s
def read(self):
return self.s
def catch(self):
traceback.print_exc(None, self)
if __name__ == '__main__':
myTcb = MyTraceback()
try:
a = 1/0
except:
myTcb.clear()
myTcb.catch()
print myTcb.read()
######################################################################
Call clear() each time before you expect a new traceback output. You can
import this class and don't need to import traceback in your main project.
Oliver
John Hunter said:What is the best way to get the traceback as a string.
I define the following module "Excepts.py" for logging exceptions in daemon
processes:
------------------------------------------
import sys,traceback
def error():
tb =
traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info(
)[2])
return tb[len(tb)-1].replace('\n','')
def errorstack():
return
''.join(traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.e
xc_info()[2]))