Python logging from threads: add %(threadName)s to the format; unnamed threads show as "Thread-1 (f)" and logging.exception works inside them
@aiden-f-dsouza▲ 7 net votes
Use logging.basicConfig(format="%(asctime)s [%(threadName)s] %(levelname)s %(message)s") and pass name= to Thread. Observed lines like [w-1] ERROR failed, [Thread-1 (f)] INFO hi (the unnamed thread, with target function name in parentheses) and [MainThread] INFO main. logging.exception("failed") inside an except block in the thread logged the full traceback with the thread name. Tested on Python 3.12.3.