Skip to content

Commit e0a409b

Browse files
authored
Add a custom Django LOGGING configuration (#250)
Since by default full Django logs are only emitted when `DEBUG=True` (which otherwise makes diagnosing errors much harder in production): https://docs.djangoproject.com/en/5.1/ref/logging/#default-logging-configuration https://docs.djangoproject.com/en/5.1/howto/logging/ GUS-W-17618372.
1 parent c639ff4 commit e0a409b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

gettingstarted/settings.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,42 @@
196196
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
197197

198198
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
199+
200+
# Customise the default logging config, since by default full Django logs are only emitted when
201+
# `DEBUG=True` (which otherwise makes diagnosing errors much harder in production):
202+
# https://docs.djangoproject.com/en/5.1/ref/logging/#default-logging-configuration
203+
# For more advanced logging you may want to try: https://django-structlog.readthedocs.io
204+
LOGGING = {
205+
"version": 1,
206+
"disable_existing_loggers": False,
207+
"formatters": {
208+
"simple": {
209+
"format": "[{levelname}] {message}",
210+
"style": "{",
211+
},
212+
},
213+
"handlers": {
214+
"console": {
215+
"class": "logging.StreamHandler",
216+
"formatter": "simple",
217+
},
218+
},
219+
# Fallback for anything not configured via `loggers`.
220+
"root": {
221+
"handlers": ["console"],
222+
"level": "INFO",
223+
},
224+
"loggers": {
225+
"django": {
226+
"handlers": ["console"],
227+
"level": "INFO",
228+
# Prevent double logging due to the root logger.
229+
"propagate": False,
230+
},
231+
"django.request": {
232+
# Suppress the WARNINGS from any HTTP 4xx responses (in particular for 404s caused by
233+
# web crawlers), but still show any ERRORs from HTTP 5xx responses/exceptions.
234+
"level": "ERROR",
235+
},
236+
},
237+
}

0 commit comments

Comments
 (0)