#36380: SQL is formatted for logging regardless of whether it will be logged -------------------------------------+------------------------------------- Reporter: Jacob Walls | Owner: Jacob | Walls Type: Bug | Status: assigned Component: Database layer | Version: dev (models, ORM) | Severity: Release blocker | Resolution: Keywords: debug-sql | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Comment (by Jacob Walls):
> Have we considered moving the formatting logic to an actual logging formater instead? I looked into this. Unless I'm missing something, it's not a good fit for our needs, because we don't want to actually change the `DEFAULT_LOGGING`. To get a `logging.Formatter` working, I have this in Django: {{{#!diff diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py index 568f510a67..fe5b9478ac 100644 --- a/django/db/backends/utils.py +++ b/django/db/backends/utils.py @@ -151,7 +151,7 @@ class CursorDebugWrapper(CursorWrapper): logger.debug( "(%.3f) %s; args=%s; alias=%s", duration, - self.db.ops.format_debug_sql(sql), + sql, params, self.db.alias, extra={ @@ -159,6 +159,7 @@ class CursorDebugWrapper(CursorWrapper): "sql": sql, "params": params, "alias": self.db.alias, + "sql_format_function": self.db.ops.format_debug_sql, }, ) diff --git a/django/utils/log.py b/django/utils/log.py index a25b97a7d5..09bb7ace50 100644 --- a/django/utils/log.py +++ b/django/utils/log.py @@ -255,3 +255,9 @@ def log_response( exc_info=exception, ) response._has_been_logged = True + + +class DebugSQLFormatter(logging.Formatter): + def format(self, record): + record.sql = record.sql_format_function(record.sql) + return super().format(record) }}} And this in my project settings. I've started with a working example of query logging and commented out the changes I'd have to make on upgrade to keep the current behavior. (As far as I can tell, there is no way to "inherit" these definitions from Django.) {{{#!py LOGGING = { "version": 1, "disable_existing_loggers": False, # "formatters": { # had to be added # "django.db.backends": { # "()": "django.utils.log.DebugSQLFormatter", # "format": "({duration:.3f}) {sql}; args={args}; alias={alias}", # "style": "{", # }, # }, "handlers": { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': 'general.log', }, # "django.db.backends": { # had to be added # "class": "logging.FileHandler", # "filename": "general.log", # "formatter": "django.db.backends", # }, }, "loggers": { "django.db.backends": { "handlers": ["file"], # "handlers": ["django.db.backends"], # had to be adjusted "level": "DEBUG", }, }, } }}} ---- Wouldn't the suggestion to use `lazy()` be preferable to maintain the prior behavior without any impact on projects? -- Ticket URL: <https://code.djangoproject.com/ticket/36380#comment:3> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/django-updates/01070196e0500092-f8bf4c04-90ad-4fb1-a36f-429f4e1b0395-000000%40eu-central-1.amazonses.com.