Skip to content

Instantly share code, notes, and snippets.

@exhuma
Last active August 21, 2020 05:32
Show Gist options
  • Save exhuma/7775c9a9888396947ff18eb1f37628ff to your computer and use it in GitHub Desktop.
Save exhuma/7775c9a9888396947ff18eb1f37628ff to your computer and use it in GitHub Desktop.
WSGI middleware setting up a queue log handler
import logging
import sys
from logging.handlers import QueueHandler, QueueListener
from queue import Queue
from time import sleep
class FlushingHandler(QueueHandler):
def flush(self):
super().flush()
pending = self.queue.qsize()
lastprint = None
while pending > 0:
if lastprint != pending:
print("Flushing %d pending log records..." % pending, file=sys.stderr)
lastprint = pending
sleep(0.2)
pending = self.queue.qsize()
self.queue.join()
class LogMiddleware:
def __init__(self, app):
self.app = app
self.queue = Queue(-1)
self.handler = FlushingHandler(self.queue)
self.log_handler = logging.StreamHandler()
self.listener = QueueListener(self.queue, self.log_handler)
self.started = False
self.prepared = False
def prepare(self):
if self.prepared:
return
logging.getLogger().addHandler(self.handler)
self.prepared = True
def __call__(self, environ, start_response):
self.prepare()
if not self.started:
print("starting...")
self.listener.start()
self.started = True
environ["LOG_MIDDLEWARE_HANDLER"] = self.handler
return self.app(environ, start_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment