Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Created January 4, 2023 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PandaWhoCodes/07e308e8a9a428a2fca01a492bf848d9 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/07e308e8a9a428a2fca01a492bf848d9 to your computer and use it in GitHub Desktop.
import logging
import time
import threading
import io
def log_info(message,logger):
logger.info(message)
time.sleep(3)
logger.info(message)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
stream = io.StringIO()
ch = logging.StreamHandler(stream)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
threads = []
for i in range(10):
t = threading.Thread(
target=log_info, args=("Hello from thread {}".format(i),logger)
)
threads.append(t)
# Start the threads
for t in threads:
t.start()
# Wait for the threads to complete
for t in threads:
t.join()
log_contents = stream.getvalue()
stream.close()
print(log_contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment