0

Is there a way to alter the print statement in Python to include the thread name before any printed message?

Currently my print statements look like

print("[%s] Some text... " % self.name)
>> [thread_name] Some text...

I have a multithreaded web server where I track some behavior from each thread and it quickly gets tedious adding the above pattern to each print statement. Is there a quick way to redefine the print() function like this?

4
  • 2
    Perhaps by using logging instead of print(...). See docs.python.org/3/howto/… Commented Nov 26, 2020 at 13:41
  • 1
    This might be useful for you: stackoverflow.com/questions/22643337/… Commented Nov 26, 2020 at 13:41
  • I assume you have a common class that you're instantiating for each thread? Or where exactly is self.name coming from? Commented Nov 26, 2020 at 13:46
  • @KarlKnechtel Yes, each thread is a class instance inheriting from threading.Thread, where I self.name them according to what they do. Commented Nov 26, 2020 at 14:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.