Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Last active December 2, 2022 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/033ee3f7005cd6099d2fb4a114cffd34 to your computer and use it in GitHub Desktop.
Save morenoh149/033ee3f7005cd6099d2fb4a114cffd34 to your computer and use it in GitHub Desktop.
Django log slow queries
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'slow_queries': {
'()': 'django.utils.log.CallbackFilter',
# output slow queries only, unit are 1s, so 0.3 is 300ms
# ref 300ms * 0.001 = 0.3, 50ms * 0.001 = 0.05
'callback': lambda record: record.duration > 0.05
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
'django.db.backends': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
'filters': ['slow_queries'],
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment