add logger in setting & request formatter
This commit is contained in:
32
Rasaddam_Backend/request_formatter.py
Normal file
32
Rasaddam_Backend/request_formatter.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from django.http import HttpRequest
|
||||||
|
|
||||||
|
|
||||||
|
class RequestFormatter(logging.Formatter):
|
||||||
|
def format(self, record):
|
||||||
|
request = getattr(record, 'request', None)
|
||||||
|
|
||||||
|
if isinstance(request, HttpRequest):
|
||||||
|
# مرحله ۱: از X-Forwarded-For (در صورت وجود)
|
||||||
|
ip = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||||
|
|
||||||
|
# مرحله ۲: از REMOTE_ADDR
|
||||||
|
if not ip:
|
||||||
|
ip = request.META.get('REMOTE_ADDR')
|
||||||
|
|
||||||
|
# مرحله ۳: اگر هنوز چیزی نیست، مقدار پیشفرض بده (مثلاً localhost)
|
||||||
|
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)
|
||||||
@@ -17,6 +17,8 @@ from pathlib import Path
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
from Rasaddam_Backend.request_formatter import RequestFormatter
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
@@ -452,3 +454,43 @@ JAZZMIN_SETTINGS = { # noqa
|
|||||||
# Add a language dropdown into the admin
|
# Add a language dropdown into the admin
|
||||||
# "language_chooser": True,
|
# "language_chooser": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGING = {
|
||||||
|
'version': 1,
|
||||||
|
'disable_existing_loggers': False,
|
||||||
|
|
||||||
|
'formatters': {
|
||||||
|
'verbose': {
|
||||||
|
'()': RequestFormatter,
|
||||||
|
'format': '[{asctime}] {levelname} {name} | IP: {client_ip} | Path: {path} | {message}',
|
||||||
|
'style': '{',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
'handlers': {
|
||||||
|
'console': {
|
||||||
|
'class': 'logging.StreamHandler',
|
||||||
|
'formatter': 'verbose',
|
||||||
|
},
|
||||||
|
'file': {
|
||||||
|
'class': 'logging.FileHandler',
|
||||||
|
'filename': 'logs/django_requests.log',
|
||||||
|
'formatter': 'verbose',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
'loggers': {
|
||||||
|
# لاگر اصلی Django
|
||||||
|
'django': {
|
||||||
|
'handlers': ['console', 'file'],
|
||||||
|
'level': 'INFO',
|
||||||
|
'propagate': True,
|
||||||
|
},
|
||||||
|
# لاگر مربوط به درخواستها
|
||||||
|
'django.server': {
|
||||||
|
'handlers': ['console', 'file'],
|
||||||
|
'level': 'INFO',
|
||||||
|
'propagate': False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
8
logs/django_requests.log
Normal file
8
logs/django_requests.log
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[2025-10-27 12:38:02,845] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
|
||||||
|
[2025-10-27 12:40:08,839] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\Rasaddam_Backend\request_formatter.py changed, reloading.
|
||||||
|
[2025-10-27 12:40:11,076] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
|
||||||
|
[2025-10-27 12:40:33,546] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/user-relations/?role= HTTP/1.1" 200 4494
|
||||||
|
[2025-10-27 12:41:44,836] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\Rasaddam_Backend\request_formatter.py changed, reloading.
|
||||||
|
[2025-10-27 12:41:46,943] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
|
||||||
|
[2025-10-27 12:41:54,499] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/user-relations/?role= HTTP/1.1" 200 4494
|
||||||
|
[2025-10-27 12:42:00,748] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/user-relations/?role= HTTP/1.1" 200 4494
|
||||||
Reference in New Issue
Block a user