Skip to content

Instantly share code, notes, and snippets.

@KaiserKatze
Created May 13, 2018 14:53
Show Gist options
  • Select an option

  • Save KaiserKatze/ce184b4692cb1d96d12ddd5e5e82c0ee to your computer and use it in GitHub Desktop.

Select an option

Save KaiserKatze/ce184b4692cb1d96d12ddd5e5e82c0ee to your computer and use it in GitHub Desktop.
Django logging middleware
def _log(request, response):
print('''
General
Request URL: {}
Request Method: {}
Status Code: {}
Remote Address: {}
------------------------------------
Request Headers
Accept: {}
Accept-Encoding: {}
Accept-Language: {}
Connection: {}
Content-Length: {}
Content-Type: {}
Cookie: {}
Host: {}
Origin: {}
Referer: {}
User-Agent: {}
'''.format(
# Request URL
request.build_absolute_uri(),
# Request Method
request.method,
# Status Code
response.status_code,
# Remote Address
request.META.get('REMOTE_ADDR', ''),
# ------------------------------------
# Accept
request.META.get('HTTP_ACCEPT', ''),
# Accept-Encoding
request.META.get('HTTP_ACCEPT_ENCODING', ''),
# Accept-Language
request.META.get('HTTP_ACCEPT_LANGUAGE', ''),
# Connection
request.META.get('HTTP_CONNECTION', ''),
# Content-Length
request.META.get('CONTENT_LENGTH', ''),
# Content-Type
request.META.get('CONTENT_TYPE', ''),
# Cookie
request.META.get('HTTP_COOKIE', ''),
# Host
request.META.get('HTTP_HOST', ''),
# Origin
request.META.get('HTTP_ORIGIN', ''),
# Referer
request.META.get('HTTP_REFERER', ''),
# User-Agent
request.META.get('HTTP_USER_AGENT', ''),
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment