Verified Commit d6649cd1 authored by Marco Marinello's avatar Marco Marinello
Browse files

Configure logging

parent 78a27da6
Pipeline #171 failed with stage
in 1 minute and 29 seconds
from . import backend
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
from datetime import datetime
import logging
requests_logger = logging.getLogger('django.request')
class AuthenticationMiddleware(MiddlewareMixin):
......@@ -25,3 +30,13 @@ class AuthenticationMiddleware(MiddlewareMixin):
)
else:
request.user = backend.AnonymousUser()
# Check for forwarded for header
ip = f"{request.META['REMOTE_ADDR']}"
if any(["forward" in i.lower() for i in request.META]):
for i in request.META:
if "forward" in i.lower():
ip = f"{request.META['REMOTE_ADDR']} f{request.META[i]}"
# Log request
requests_logger.info(
f"{ip} {getattr(request.user, 'username', '-')} {request.method} {request.get_full_path()}"
)
......@@ -145,6 +145,75 @@ STATIC_URL = '/static/'
STATIC_ROOT = "/var/cache/octonet/static"
LOGGING = {
'version': 1,
# 'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'formatters': {
'logfile': {
'format': '[%(asctime)s %(module)s] %(levelname)s: %(message)s',
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
},
'null': {
'class': 'logging.NullHandler',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/log/octonet/octonet.log',
'when': 'midnight',
'backupCount': 365,
'filters': ['require_debug_false'],
'formatter': 'logfile',
},
},
'loggers': {
'django': {
'handlers': ['console'],
},
'django.request': {
'handlers': ['file'],
'level': 'INFO',
'propagate': True,
},
'django.security': {
'handlers': ['mail_admins', 'file'],
'level': 'ERROR',
'propagate': False,
},
'django.db.backends': {
'level': 'INFO',
'handlers': ['file'],
'propagate': True,
},
'py.warnings': {
'handlers': ['console'],
},
},
'root': {
'level': 'INFO',
'handlers': ['file', 'console'],
},
}
try:
from .local_settings import *
except ImportError:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment