Last active
February 4, 2017 01:31
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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