forked from binaryage/firelogger.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (55 loc) · 2.2 KB
/
app.py
File metadata and controls
71 lines (55 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import random
import urllib
import logging
from xml.sax import saxutils
from wsgiref.util import setup_testing_defaults
from firepython.demo._body import BODY, EXCLAMATIONS
LOGGER_NAME = __name__
class FirePythonDemoApp(object):
_did_setup_logging = False
def __init__(self, global_config=None):
if not global_config:
global_config = {}
self.global_config = global_config
self.__body__ = BODY
self.log = logging.getLogger(LOGGER_NAME)
def _setup_logging(self):
if self._did_setup_logging:
return
self.log.setLevel(logging.DEBUG)
for handler in self.log.root.handlers + self.log.handlers:
handler.setLevel(logging.DEBUG)
self._did_setup_logging = True
def __call__(self, environ, start_response):
self._setup_logging()
if 'error' in environ.get('QUERY_STRING', '').lower():
try:
busted = 10000 / 0
except Exception:
self.log.exception('OMG you cannot has division by zero!: ')
self.log.critical('It is because the Zero cannot be divided!')
self.log.error('and if you continue to WANT, will be ERROR')
self.log.warn('I am givin you dis warning!')
self.log.info('It is for your information')
self.log.debug('While this is just a bonus')
else:
self.log.info('Nothing to see here, folks')
self.log.debug('for serious ... is nothing')
start_response('200 OK', [('content-type', 'text/html')])
body = self.__body__ % dict(
environ='\n' + self._get_pretty_environ(environ),
error=urllib.quote(random.choice(EXCLAMATIONS)),
)
return [body]
def _get_pretty_environ(self, environ):
base = {'QUERY_STRING': ''}
setup_testing_defaults(base)
for key in base.keys():
base[key] = environ.get(key, base[key])
sortkeys = base.keys()
sortkeys.sort()
ret = []
for key in sortkeys:
escaped = saxutils.escape(repr(base[key]))
ret.append('%s: %s\n' % (key, escaped))
return ''.join(ret)