Created
January 4, 2023 09:28
-
-
Save PandaWhoCodes/4293d1ba716ef1419d9a8c4422963286 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 collections | |
import threading | |
import time | |
from collections import defaultdict | |
def log_info(message, logger): | |
logger.info(message) | |
time.sleep(3) | |
logger.info(message) | |
class TailLogHandler(logging.Handler): | |
def __init__(self, log_queue): | |
logging.Handler.__init__(self) | |
self.log_queue = log_queue | |
def emit(self, record): | |
thread_name = threading.Thread.getName(threading.current_thread()) | |
self.log_queue[thread_name].append(self.format(record)) | |
class TailoredLogger(object): | |
def __init__(self): | |
self._log_queue = defaultdict(list) | |
self._log_handler = TailLogHandler(self._log_queue) | |
def contents(self, thread): | |
if thread == "MainThread": | |
return self.reset_mainThread() | |
return self._log_queue[thread] | |
def get_threads(self): | |
return self._log_queue.keys() | |
def reset_mainThread(self): | |
return self._log_queue.pop("MainThread") | |
@property | |
def log_handler(self): | |
return self._log_handler | |
def print_logs_from_list(list): | |
print("\n".join(list)) | |
logger = logging.getLogger(__name__) | |
tail = TailoredLogger() | |
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | |
log_handler = tail.log_handler | |
log_handler.setFormatter(formatter) | |
logger.addHandler(log_handler) | |
logger.setLevel(logging.INFO) | |
for i in range(10): | |
logger.log(logging.INFO, "Message {}".format(i)) | |
print_logs_from_list(tail.contents("MainThread")) | |
# Create some threads | |
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() | |
for thread in tail.get_threads(): | |
print_logs_from_list(tail.contents(thread)) | |
for i in range(10): | |
logger.log(logging.INFO, "Message {}".format(i)) | |
print_logs_from_list(tail.contents("MainThread")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment