I can't get logging to work in my Django web app.
My settings file looks like this:
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 465
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "password"
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "[email protected]"
SERVER_EMAIL = 'smtp.gmail.com'
ADMINS = (
('Paul Tremblay', '[email protected]'),
)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': '/var/log/django_logs/debug.log'
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': True,
},
'django.request': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': True,
},
},
}
I have this in my views file:
def five_hundred_test(request):
raise ValueError("raising this on purpose for testing for 500")
return render(request, 'my_test/basic_css.html')
When I point my browser to this function, I get a 500 Error (as expected), but nothing gets sent to my email, and nothing gets put in the log file. I am using Django 1.9, with python3, using Apache to run the server.
This can be done much simpler. If you want to log to both to email and Apache log files, you need to do the following in settings.py
:
Configure email settings - this assures that Django can send emails
SERVER_EMAIL = '[email protected]'
EMAIL_HOST = 'smtp.me.eu'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = SERVER_EMAIL
EMAIL_HOST_PASSWORD = 'password'
Set DEBUG
to False
- this enables error email sending
DEBUG = False
Add error email receivers to ADMINS
(you might want MANAGERS, too) - this designates who receives the error emails
ADMINS = (
('Me', '[email protected]'),
)
MANAGERS = ADMINS
Finally, remove the default console filter that does not let messages through when debug mode is disabled - this enables errors to end up in Apache error logs
from django.utils.log import DEFAULT_LOGGING
# Assure that errors end up to Apache error logs via console output
# when debug mode is disabled
DEFAULT_LOGGING['handlers']['console']['filters'] = []
This has to be in settings.py
as logging will be configured with DEFAULT_LOGGING
immediately after settings have been imported.
In case you want to add support for logging from your own code, configure the root logger in settings.py
as well:
# Enable logging to console from our modules by configuring the root logger
DEFAULT_LOGGING['loggers'][''] = {
'handlers': ['console'],
'level': 'INFO',
'propagate': True
}
Finally, you may want to turn on detailed logging:
# Detailed log formatter
DEFAULT_LOGGING['formatters']['detailed'] = {
'format': '{levelname} {asctime} {pathname}:{funcName}:{lineno} {message}',
'style': '{',
}
DEFAULT_LOGGING['handlers']['console']['formatter'] = 'detailed'
You can then use it as follows:
log = logging.getLogger(__name__)
log.info('Logging works!')
This is my working setup. I have not added a console logger
, as I am using it only to production. The inspiration came from the relevant Logging Examples of Django docs.
Here are my email and logging settings and here is a link to the relevant django docs for the email backend:
from os.path import join
# Email Configuration ==========================================================
ADMINS = [('Me', 'my@email'), ]
MANAGERS = ADMINS
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'My Name To Display <my@email>'
SERVER_EMAIL = 'error_from@email'
EMAIL_HOST = 'smtp.server' # An IP may work better
EMAIL_HOST_USER = 'username_for_login'
EMAIL_HOST_PASSWORD = 'password_for_login'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
# Logging Configuration ========================================================
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': join(HOME_DIR, 'my_logs', 'debug.log'),
'formatter': 'verbose'
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'formatter': 'simple'
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': True,
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
},
}
I have spotted two errors to your environment variables:
SERVER_EMAIL
setting must be be an email address.TLS
and therefore the correct port should be 587.Moreover, you are trying to use Google as an SMTP server, but according to this updated answer this service is no longer available from google and you have to search for another SMTP server.
I have also assembled a post with a couple debugging techniques if you need to use.
Also check your email client rules on what to do with the emails once they arrive.
I had some rules in my e-mail client with errors and they were stopping my Django logging mails to enter/show up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With