Logging and email not working for Django for 500

后端 未结 3 1379
轻奢々
轻奢々 2021-01-12 18:18

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         


        
相关标签:
3条回答
  • 2021-01-12 18:59

    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:

    1. 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'
      
    2. Set DEBUG to False - this enables error email sending

      DEBUG = False
      
    3. 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
      
    4. 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!')
    
    0 讨论(0)
  • 2021-01-12 18:59

    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:

    • The SERVER_EMAIL setting must be be an email address.
    • You are using 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.

    0 讨论(0)
  • 2021-01-12 19:10

    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.

    0 讨论(0)
提交回复
热议问题