29 lines
681 B
Python
29 lines
681 B
Python
import logging
|
|
|
|
from django.http import HttpRequest
|
|
|
|
|
|
class RequestFormatter(logging.Formatter):
|
|
def format(self, record):
|
|
request = getattr(record, 'request', None)
|
|
|
|
if isinstance(request, HttpRequest):
|
|
ip = request.META.get('HTTP_X_FORWARDED_FOR')
|
|
|
|
if not ip:
|
|
ip = request.META.get('REMOTE_ADDR')
|
|
|
|
if not ip:
|
|
ip = '127.0.0.1'
|
|
|
|
if ',' in ip:
|
|
ip = ip.split(',')[0].strip()
|
|
|
|
record.client_ip = ip
|
|
record.path = request.path
|
|
else:
|
|
record.client_ip = '-'
|
|
record.path = '-'
|
|
|
|
return super().format(record)
|