Use 2 logger patterns: django and django.db.backends.
Need to make sure to add 'propagte': False to django.db.backends
Otherwise, the db log will show twice.
# Separate DB SQL log from django log
LOGGING = {
'version': 1,
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
}
},
'handlers': {
'db_handler': {
'level': 'DEBUG',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'db_format'
},
'log_handler': {
'level': 'DEBUG',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'log_format'
}
},
'loggers': {
'django': {
'handlers': ['log_handler'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
'django.db.backends': {
'level': 'DEBUG',
'handlers': ['db_handler'],
'propagate': False
},
},
'formatters': {
'db_format': {
'format': 'sql> %(message)s'
},
'log_format': {
'format': 'django> %(message)s'
}
}
}
In a api request, the console show logs like this:
Django version 3.2.3, using settings 'bookstore_openapi.settings'
Starting development server at http://0.0.0.0:8001/
Quit the server with CTRL-BREAK.
sql> (0.000) SELECT "book_book"."id", "book_book"."title", "book_book"."category_id", "book_book"."publisher_id", "book_book"."author_id" FROM "book_book" WHERE "book_book"."id" = 1 LIMIT 21; args=(1,)
django> "GET /api/books/1 HTTP/1.1" 200 72
No comments:
Post a Comment