Skip to content

Commit d353732

Browse files
committed
refactor: add django logging configuration with sanitization
1 parent a614f92 commit d353732

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

website/settings.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,113 @@
176176
CORS_ALLOW_ALL_ORIGINS = True
177177
else:
178178
CORS_ALLOW_ALL_ORIGINS = False
179+
180+
181+
# ==============================================================================
182+
# LOGGING CONFIGURATION
183+
# ==============================================================================
184+
import logging
185+
from lib.logging import SanitizationFilter, add_sanitization_to_logger
186+
187+
DEFAULT_LOG_FORMAT = "[%(asctime)s][%(name)s:%(lineno)d][%(levelname)s] - %(message)s"
188+
# or DEFAULT_LOG_FORMAT = "[%(asctime)s][%(process)d:%(thread)d][%(name)s:%(lineno)d][%(levelname)s] - %(message)s" ?
189+
JSON_LOG_FORMAT = {
190+
"asctime": "%(asctime)s",
191+
"name": "%(name)s",
192+
"lineno": "%(lineno)d",
193+
"levelname": "%(levelname)s",
194+
"message": "%(message)s",
195+
"process": "%(process)d",
196+
"thread": "%(thread)d",
197+
}
198+
199+
LOG_DIR = BASE_DIR / "logs"
200+
LOG_DIR.mkdir(mode=0o755, parents=True, exist_ok=True)
201+
202+
LOGGING = {
203+
"version": 1,
204+
"disable_existing_loggers": False,
205+
206+
"filters": {
207+
"sanitization_filter": {
208+
"()": "lib.logging.SanitizationFilter",
209+
"max_length": 1000,
210+
}
211+
},
212+
"formatters": {
213+
"verbose": {
214+
"format": DEFAULT_LOG_FORMAT,
215+
"datefmt": "%Y-%m-%d %H:%M:%S",
216+
},
217+
"simple": {
218+
"format": "[%(levelname)s] %(message)s"
219+
},
220+
},
221+
"handlers": {
222+
"console": {
223+
"level": "DEBUG" if DEBUG else "INFO",
224+
"class": "logging.StreamHandler",
225+
"formatter": "verbose",
226+
"filters": ["sanitization_filter"],
227+
},
228+
"file": {
229+
"level": "DEBUG" if DEBUG else "INFO",
230+
"class": "logging.handlers.RotatingFileHandler",
231+
"filename": LOG_DIR / "django.log",
232+
"maxBytes": 10 * 1024 * 1024,
233+
"backupCount": 5,
234+
"formatter": "verbose",
235+
"filters": ["sanitization_filter"],
236+
"encoding": "utf-8",
237+
},
238+
"error_file": {
239+
"level": "ERROR",
240+
"class": "logging.handlers.RotatingFileHandler",
241+
"filename": LOG_DIR / "error.log",
242+
"maxBytes": 10 * 1024 * 1024,
243+
"backupCount": 5,
244+
"formatter": "verbose",
245+
"filters": ["sanitization_filter"],
246+
"encoding": "utf-8",
247+
},
248+
},
249+
"loggers": {
250+
"django": {
251+
"handlers": ["console", "file", "error_file"],
252+
"level": "INFO",
253+
"propagate": False,
254+
},
255+
"django.request": {
256+
"handlers": ["console", "file", "error_file"],
257+
"level": "ERROR",
258+
"propagate": False,
259+
},
260+
"apps": {
261+
"handlers": ["console", "file", "error_file"],
262+
"level": "DEBUG" if DEBUG else "INFO",
263+
"propagate": False,
264+
},
265+
"lib": {
266+
"handlers": ["console", "file", "error_file"],
267+
"level": "INFO",
268+
"propagate": False,
269+
},
270+
"django.db.backends": {
271+
"handlers": ["console", "error_file"],
272+
"level": "ERROR" if not DEBUG else "INFO",
273+
"propagate": False,
274+
},
275+
},
276+
"root": {
277+
"handlers": ["console", "file"],
278+
"level": "INFO",
279+
},
280+
}
281+
def initialize_logging():
282+
# Optimize log levels in production
283+
if not DEBUG:
284+
logging.getLogger("django").setLevel(logging.WARNING) # reduce verbosity
285+
logging.getLogger("django.request").setLevel(logging.ERROR)
286+
logging.getLogger("django.db.backends").setLevel(logging.ERROR)
287+
logging.getLogger("django.security").setLevel(logging.INFO)
288+
initialize_logging() # Call the initialization function at the startup

0 commit comments

Comments
 (0)