I have the following test code:
import logging
def main():
l = ['a', 'b']
l.index('c')
logging.basicConfig(level=logging.DEBUG, filename='myapp.log', format='')
try:
main()
except:
logging.exception('')
When executed (in Python 2.7.10 under Windows 7), it outputs to the log file the following traceback info:
Traceback (most recent call last):
File "C:/Documents and Settings/maurobio/test/log.py", line 12, in <module>
main()
File "C:/Documents and Settings/maurobio/test/log.py", line 6, in main
l.index('c')
ValueError: 'c' is not in list
I have a twofold question:
Is there a way to suppress the exception context and get only the last traceback?
How could I get only the basename (name + extension) of the file raising the exception, instead of the full name with directory?
To sum up: I would like to output to the log file just something like:
Traceback (most recent call last):
File "log.py", line 6, in main
l.index('c')
ValueError: 'c' is not in list
Thanks in advance for any assistance you can provide.