Skip to content

Commit

Permalink
feat(debug): add SQL query debugging option
Browse files Browse the repository at this point in the history
Added two environment variables `DEBUG` and `DEBUG_SQL_QUERY` for
debugging. The `DEBUG_SQL_QUERY` variable is separated to control
verbose SQL query logging. Updated settings to handle these variables
and configure logging accordingly.
  • Loading branch information
WilsonNet committed Sep 17, 2024
1 parent f54f09c commit a584ded
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
7 changes: 7 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ We are not using sessions or anything like that right now, so changing the secre

# Requests
In the `/requests` directory we have scripts that execute requests to endpoints using [httpie](https://httpie.io/)


# Debug

For debugging we have two env variables

`DEBUG` and `DEBUG_SQL_QUERY` that can be set to `True` to enable debugging. The reason `DEBUG_SQL_QUERY` is separated is that it can be very verbose.
41 changes: 38 additions & 3 deletions backend/kernelCI/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def get_json_env_var(name, default):
if isBooleanOrStringTrue(ENV_DEBUG):
DEBUG = True

DEBUG_SQL_QUERY = False

ENV_DEBUG_SQL_QUERY = get_json_env_var("DEBUG_SQL_QUERY", False)

if isBooleanOrStringTrue(ENV_DEBUG_SQL_QUERY) and DEBUG:
DEBUG_SQL_QUERY = True

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
Expand Down Expand Up @@ -127,9 +133,9 @@ def get_json_env_var(name, default):


CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'ecom',
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "ecom",
}
}

Expand Down Expand Up @@ -196,4 +202,33 @@ def __getitem__(self, item):
SECURE_SSL_REDIRECT = False
SECURE_HSTS_SECONDS = 3600

if DEBUG_SQL_QUERY:
LOGGING = {
"disable_existing_loggers": False,
"version": 1,
"handlers": {
"console": {
# logging handler that outputs log messages to terminal
"class": "logging.StreamHandler",
"level": "DEBUG", # message level to be written to console
},
},
"loggers": {
"": {
# this sets root level logger to log debug and higher level
# logs to console. All other loggers inherit settings from
# root level logger.
"handlers": ["console"],
"level": "DEBUG",
"propagate": False, # this tells logger to send logging message
# to its parent (will send if set to True)
},
"django.db": {
# django also has database level logging
"level": "DEBUG"
},
},
}


CACHE_TIMEOUT = int(get_json_env_var("CACHE_TIMEOUT", "600"))

0 comments on commit a584ded

Please sign in to comment.