Take a look at logging.exception
(Python Logging Module)
import logging
def foo():
try:
some_code()
except:
logging.exception('')
This should automatically take care of getting the traceback for the current exception and logging it properly.