Skip to content

Instantly share code, notes, and snippets.

@vsajip
Created September 16, 2010 10:59
Show Gist options
  • Save vsajip/582259 to your computer and use it in GitHub Desktop.
Save vsajip/582259 to your computer and use it in GitHub Desktop.
import logging
import time
logger = logging.getLogger(__name__)
def useful():
logger.debug('Hello from webapplib!')
time.sleep(0.01)
import logging
from random import choice
import threading
import webapplib
logger = logging.getLogger(__name__)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
class Request:
def __init__(self, method, ip):
self.method = method
self.ip = ip
REQUESTS = [
Request('GET', '192.168.2.20'),
Request('POST', '192.168.2.20'),
Request('GET', '192.168.2.21'),
Request('POST', '192.168.2.21'),
Request('GET', '192.168.2.22'),
Request('POST', '192.168.2.22'),
]
formatter = logging.Formatter('%(asctime)s %(threadName)-11s %(appName)s %(name)-9s %(ip)s %(method)-4s %(message)s')
tlocal = threading.local()
class InjectingFilter(logging.Filter):
def __init__(self, app):
self.app = app
def filter(self, record):
record.method = tlocal.request.method
record.ip = tlocal.request.ip
record.appName = appName = tlocal.appName
return appName == self.app.name
class WebApp:
def __init__(self, name):
self.name = name
handler = logging.FileHandler(name + '.log', 'w')
f = InjectingFilter(self)
handler.setFormatter(formatter)
handler.addFilter(f)
root.addHandler(handler)
self.num_requests = 0
def process_request(self, request):
tlocal.request = request
tlocal.appName = self.name
tname = threading.currentThread().getName()
self.num_requests += 1
logger.debug('Request processing started')
webapplib.useful()
logger.debug('Request processing finished')
def main():
app1 = WebApp('app1')
app2 = WebApp('app2')
apps = [app1, app2]
threads = []
#Add a common handler which will capture all events
handler = logging.FileHandler('app.log', 'w')
handler.setFormatter(formatter)
root.addHandler(handler)
#while True:
for i in xrange(1000):
try:
app = choice(apps)
request = choice(REQUESTS)
t = threading.Thread(target=app.process_request, args=(request,))
threads.append(t)
t.start()
except KeyboardInterrupt:
break
for t in threads:
t.join()
for app in apps:
print '%s processed %s requests' % (app.name, app.num_requests)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment