Skip to content

Instantly share code, notes, and snippets.

@JasonKessler
Last active February 4, 2017 01:31
Show Gist options
  • Save JasonKessler/8563282fcedf010bc809e24f647b780b to your computer and use it in GitHub Desktop.
Save JasonKessler/8563282fcedf010bc809e24f647b780b to your computer and use it in GitHub Desktop.
Apache combined logging format for Flask/Python3 (poorly hacked together and someone incomplete)
def apache_combined_log(request: flask.request):
path = request.path
if len(request.query_string) > 0:
path += '?' + request.query_string.decode('utf-8')
size = '-'
if request.__sizeof__() > 0:
size = request.__sizeof__()
referrer = '-'
if request.referrer:
referrer = request.referrer
request_line = '"' + request.method + ' ' + path + ' ' + request.environ['SERVER_PROTOCOL'] + '"'
log_entry = ' '.join([str(x) for x
in [request.remote_addr,
'-',
'-',
strftime('[%d/%b/%Y:%H:%M:%S %z]', gmtime()),
'"%s"' % request_line,
200,
size,
'"%s"' % referrer,
'"%s"' % request.user_agent]])
return log_entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment