Skip to content

Instantly share code, notes, and snippets.

@8bitbuddhist 8bitbuddhist/ Secret
Created Feb 6, 2019

What would you like to do?
Synthetic transaction monitoring demonstrating using WSGI
import gettext
import logging.handlers
from logging.handlers import SysLogHandler
import socket
class ContextFilter(logging.Filter):
hostname = socket.gethostname()
def filter(self, record):
record.hostname = ContextFilter.hostname
return True
# Initialize logging components
syslogHandler = SysLogHandler(address=('', XXXXX))
syslogFormat = '%(asctime)s %(hostname)s [%(levelname)s] (%(funcName)s) %(message)s'
formatter = logging.Formatter(syslogFormat, datefmt='%b %d %H:%M:%S')
logger = logging.getLogger()
logger.debug('Starting WSGI app')
def application(environ, start_response):
logger.debug('Handling request. Environment vars: %s', environ)
# If the locale is not present in the URL, redirect the user
locale = environ['PATH_INFO'][1:3]
if not locale:
newlocale = environ['HTTP_ACCEPT_LANGUAGE'][0:2]'Switching to locale: %s', newlocale)
start_response('302 Found',
[('Location', 'http://localhost:8000/' + newlocale)])
return [b'1']'Locale: %s', locale)
translation = gettext.translation('app', localedir='locale', languages=[locale])
output = _('Hello world!')
status = '200 OK''Got localization string: %s', output)
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [bytes(output, 'utf-8')]
except OSError as e:
logger.exception('Localization error')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.