Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Created January 4, 2023 09:21
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/0d199f766de4dc59fd8908ff1a3e1445 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/0d199f766de4dc59fd8908ff1a3e1445 to your computer and use it in GitHub Desktop.
import logging
import time
import threading
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):
return self._log_queue[thread]
def get_threads(self):
return self._log_queue.keys()
@property
def log_handler(self):
return self._log_handler
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)
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("\n".join(tail.contents(thread)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment