Aracılığıyla paylaş


Debug Django Web Application in Azure Web Apps

After creating a Azure Web/API app using Django Framework in Python, you may end-up getting Application errors at some point of your Application Development Process. This Blog describes how to enable Application logs for a Azure Web/API APP which uses Django Framework.

 

Before starting debug process make sure that you have below two important files at Web/API APP Root folder. You can find them in wwwroot folder @ kudu console(https://Your_Website.scm.azurewebsites.net)

  web.config 
 ptvs_virtualenv_proxy.py

Please find more information on above files @ https://azure.microsoft.com/en-us/documentation/articles/web-sites-python-configure/#webconfig

 

You can display Django Application errors using below three channels

1) Display Application Errors on Web Browser

- Open settings.py file in your Django Web App

- In Settings.py file change debug option to True as below

 DEBUG = True

Output :

As you can see in above screen shot, Application error info is shown on web page with error Traceback.

It is highly recommended to Swtich DEBUG option to False when you move the site into Production.

 

2) Log Errors in a Azure File System.

- In settings.py file add below content for LOGGING variable

 LOGGING = {
 'version': 1,
 'disable_existing_loggers': False,
 'filters': {
 'require_debug_false': {
 '()': 'django.utils.log.RequireDebugFalse'
 }
 },
 'handlers': {
 'logfile': {
 'class': 'logging.handlers.WatchedFileHandler',
 'filename': 'D:\home\site\wwwroot\myapp.log'
 }
 },
 'loggers': {
 'django': {
 'handlers': ['logfile'],
 'level': 'ERROR',
 'propagate': False,
 }
 }
 }

- You can find more information on LOGGING module @ https://docs.djangoproject.com/en/dev/topics/logging/

Output :

After making above changes, You would see myapp.log file in wwwroot folder as in Below screenshot.

 

Below Screenshot has the error message in myapp.log file.

 

This option is highly suitable for websites in production.

 

3) Email Application Errors to Developers

- In settings.py file add below content for LOGGING variable. I have highlighted configuration which is required to send an Email in RED.

 LOGGING = {
 'version': 1,
 'disable_existing_loggers': False,
 'filters': {
 'require_debug_false': {
 '()': 'django.utils.log.RequireDebugFalse'
 }
 },
 'handlers': {
 'mail_admins': {
 'level': 'ERROR',
 'filters': ['require_debug_false'],
 'class': 'django.utils.log.AdminEmailHandler',
 'include_html': True
 },
 'logfile': {
 'class': 'logging.handlers.WatchedFileHandler',
 'filename': 'D:\home\site\wwwroot\myapp.log'
 }
 },
 'loggers': {
 'django.request': {
 'handlers': ['mail_admins'],
 'level': 'ERROR',
 'propagate': True,
 },
 'django': {
 'handlers': ['logfile'],
 'level': 'ERROR',
 'propagate': False,
 }
 }
 }

- Include smtp connection details in settings.py file as below

  EMAIL_USE_TLS = True
 EMAIL_HOST = 'smtp.gmail.com'
 EMAIL_PORT = 587
 EMAIL_HOST_USER = 'Your_GMAIL@gmail.com'
 EMAIL_HOST_PASSWORD = 'Your_GMAIL_Password'

- Change recipient email address in setting.py file as Below

 ADMINS = (
 ('Prashanth madi', 'prmadi@microsoft.com'),
 )

Output :

A email would be sent to recipients mentioned above when a application error occurs. Below is a Sample Application error message which I received for my website.

 

You can also send an email in html format by including below line of code in logger handler. Below is a Sample Application error message which I received for my website in HTML format.

 'include_html': True

 

Find more details on using Python in Azure Web/API Apps at below links :  

- Python Developer Center - https://azure.microsoft.com/en-us/develop/python/ 

- Configuring Python in Azure Web Apps - https://azure.microsoft.com/en-us/documentation/articles/web-sites-python-configure/

- Troubleshoot- logging Fatal Errors of Azure Web/API Apps in Python - https://blogs.msdn.com/b/azureossds/archive/2015/07/15/troubleshoot-logging-python-application-errors-on-azure-web-api-apps.aspx

Comments

  • Anonymous
    August 31, 2015
    if use Python 3.4, then will be fail and won't write anything to the log file. >_<

  • Anonymous
    September 01, 2015
    Hi vitorywu, Could you provide us more details on what dint worked for you . Do you see any error ? Can you try my other blog on logging fatal error and see if that works.

  • Anonymous
    September 01, 2015
    Hi Prashanth .. Thanks for your help ^^ I have do this "Troubleshoot- logging Fatal Errors of Azure Web/API Apps in Python" and log content on the below .. D:homesitewwwroot>more logs.txt 2015-09-02 01:13:01.146216: wfastcgi.py 2.1.1 started 2015-09-02 01:13:01.193088: Python version: 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] 2015-09-02 01:13:01.208715: wfastcgi.py 2.1.1 initializing 2015-09-02 01:13:01.271212: Activating venv with executable at D:homesitewwwrootenvScriptspython.exe 2015-09-02 01:13:01.349338: Getting handler django.core.wsgi.get_wsgi_application() 2015-09-02 01:13:06.283218: Got handler: <django.core.handlers.wsgi.WSGIHandler object at 0x021E0630> 2015-09-02 01:13:06.298840: wfastcgi.py will restart when files in D:homesitewwwroot are changed: .*((.py)|(.config))$ 2015-09-02 01:13:06.298840: wfastcgi.py 2.1.1 initialized

I show you my settings.py about LOGGING setting part ..

from os import path PROJECT_ROOT = path.dirname(path.abspath(path.dirname(file))) SECRET_KEY = 'key ^^' DEBUG = True

DEBUG = False

#TEMPLATE_DEBUG = True ALLOWED_HOSTS = ['this is our domain ^^'] LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' } }, 'handlers': { 'logfile': { 'class': 'logging.handlers.WatchedFileHandler', 'filename': 'D:homesitewwwrootdebug.log' } }, 'loggers': { 'django': { 'handlers': ['logfile'], 'level': 'ERROR', 'propagate': False, } } }

finally .. I saw the log file "debug.log" on the wwwroot but no any content, so I don't know how to fix it problem :(

  • Anonymous
    September 02, 2015
    The comment has been removed
  • Anonymous
    November 16, 2016
    Hi Prashanth, Thanks for this description of logging.But I got some confuse question for writing log.below are my setting.py:---------------------------------------DEBUG = TrueTEMPLATE_DEBUG = DEBUG.....LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' }, 'logfile': { 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.getenv('LOGFILE', 'django.log') } }, 'loggers': { 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': True, }, 'django': { 'handlers': ['logfile'], 'level': 'INFO', 'propagate': False, } }}--------------------------------------------** I want passes all INFO messages to django.log.It will correctly write django.log when I running server at localhost test.After I publish to Azure web app service, and set the "Application setting " => "App settings", add new path LOGFILE D:\home\site\wwwroot\logfiles\django.log.I send a few request to my app service, then log in DebugConsole to watch the D:\home\site\wwwroot\logfiles\django.log file.There has no any content in this log file (django.log), and I try to modify it at DebugConsole, it will occurs ERROR 409 confict: Could not write to local resource 'D:\home\site\wwwroot\logfiles\django.log' due to error 'The process cannot access the file 'D:\home\site\wwwroot\logfiles\django.log' becaure it is being used by another process.'.It seem like Django cannot write anything in django.log, how can I do?Could you help for this problem?
    • Anonymous
      November 18, 2016
      Hi @chouvaalI would suggest you to try hosting django app with httpplatformhandler. This has been giving better performance with less configuration during deploymenthttp://prmadi.com/django-app-with-httpplatformhandler-in-azure-app-services-windows/