Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
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
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.
from os import path PROJECT_ROOT = path.dirname(path.abspath(path.dirname(file))) SECRET_KEY = 'key ^^' DEBUG = True
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 :(
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in