Created
January 4, 2023 09:16
-
-
Save PandaWhoCodes/07e308e8a9a428a2fca01a492bf848d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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