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_USE
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 = 'me@me.eu'
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', 'me@me.eu'),
)
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
}
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:
from os.path import join
# Email Configuration ==========================================================
ADMINS = [('Me', 'my@email'), ]
MANAGERS = ADMINS
DEFAULT_FROM_EMAIL = 'from@email'
SERVER_EMAIL = 'error_from@email'
EMAIL_HOST = 'smtp.server'
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:
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.