Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date & time of exceptions in Python errors

Sometimes it would be happened that my services (which started manually in tmux or screen) will be stop by getting an unknown or other kinds of exceptions that I didn't handle them. In my case date & time of exceptions event is so important.

So how to get date & time of exceptions in Python errors? Like running a service in tmux or screen and wait to see the results which it will print.

like image 431
mortymacs Avatar asked Dec 14 '25 02:12

mortymacs


1 Answers

To solve this issue, we can try to use sys.excepthook like so:

import sys
import traceback
import datetime

def err(type, value, tb):
    print("Exception date time: {}".format(datetime.datetime.now()))
    print(traceback.print_tb(tb))

sys.excepthook = err
like image 109
mortymacs Avatar answered Dec 15 '25 16:12

mortymacs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!