How can I get Django to print the debug information to the console?

后端 未结 6 1827
故里飘歌
故里飘歌 2020-12-04 19:57

I\'m using urlib to hit my app not a browser so I can\'t see the debug screen when an error occurs. What\'s the best way to send the normal debug info to the console or a fi

6条回答
  •  情书的邮戳
    2020-12-04 20:14

    The best way to do this is to set up a logging handler for django.requests. You do this by adding the LOGGING setting to your settings.py file. An example is included below:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'verbose': {
                'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
            },
        },
        'handlers': {
            'console': {
                'level': 'NOTSET',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose'
            }
        },
        'loggers': {
            '': {
                'handlers': ['console'],
                'level': 'NOTSET',
            },
            'django.request': {
                'handlers': ['console'],
                'propagate': False,
                'level': 'ERROR'
            }
        }
    }
    

    With this in place, you'll see a nice stack trace in your terminal every time a 500 response is returned.

    For more information on Django's implementation of the Python logger, see the documentation about logging.

提交回复
热议问题