08.配置django发送log邮件

https://docs.djangoproject.com/en/3.0/topics/email/
https://docs.djangoproject.com/en/3.0/topics/email/#topic-email-backends
sentry 不香么

一.网上的

https://www.sohu.com/a/193738029_236714

  • settings - hannder
 'mail_admin': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': tRUE,
        },
  • settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.xxxx.com.cn'  # SMTP地址
EMAIL_PORT = 25  # SMTP端口
EMAIL_HOST_USER = 'xxxx@xxxx.com.cn'  # 发送邮件的【邮箱账户】
EMAIL_HOST_PASSWORD = 'xxxxxx'  # 发送邮件的【邮箱密码】
EMAIL_SUBJECT_PREFIX = u'[prefix]'  # 为邮件Subject-line前缀,默认是'[django]'
EMAIL_USE_TLS = False  # 与SMTP服务器通信时,是否启动TLS链接(安全链接)。默认是false
SERVER_EMAIL = 'webmaster@xxxx.com.cn' 
ADMINS = (('webmaster','webmaster@xxxx.com.cn'),)  # 接收邮件的邮箱(或邮件组)
  • 邮件内容
# view  logger.error('发送到邮件的日志')
# title [django_ops]ERROR: 发送到邮件的日志
# 邮件内容
发送到邮件的日志

Report
发送到邮件的日志

Django Version: 3.0.6
Python Executable: /home/jenvid/PycharmProjects/01myproject/ops_env/bin/python3.7
Python Version: 3.7.5
Python Path: ['/home/jenvid/PycharmProjects/django/ops/apps', '/home/jenvid/PycharmProjects/django/ops', '/usr/local/pycharm-2019.3.1/plugins/python/helpers/pycharm_display', '/home/jenvid/PycharmProjects/01myproject/ops_env/lib/python37.zip', '/home/jenvid/PycharmProjects/01myproject/ops_env/lib/python3.7', 

二.参数

https://docs.djangoproject.com/en/3.0/topics/logging/#django.utils.log.AdminEmailHandler
https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-EMAIL_HOST

Email¶
ADMINS
DEFAULT_CHARSET
DEFAULT_FROM_EMAIL
EMAIL_BACKEND
EMAIL_FILE_PATH
EMAIL_HOST
EMAIL_HOST_PASSWORD
EMAIL_HOST_USER
EMAIL_PORT
EMAIL_SSL_CERTFILE
EMAIL_SSL_KEYFILE
EMAIL_SUBJECT_PREFIX
EMAIL_TIMEOUT
EMAIL_USE_LOCALTIME
EMAIL_USE_TLS
MANAGERS
SERVER_EMAIL
  • ADMINS
Default: [] (Empty list)

A list of all the people who get code error notifications. When DEBUG=False and AdminEmailHandler is configured in LOGGING (done by default), Django emails these people the details of exceptions raised in the request/response cycle.

Each item in the list should be a tuple of (Full name, email address). Example:
[('John', 'john@example.com'), ('Mary', 'mary@example.com')]
  • DEFAULT_CHARSE
Default: 'utf-8'

Default charset to use for all HttpResponse objects, if a MIME type isn’t manually specified. Used when constructing the Content-Type header.
  • DEFAULT_FROM_EMAIL
Default: 'webmaster@localhost'

Default email address to use for various automated correspondence from the site manager(s). This doesn’t include error messages sent to ADMINS and MANAGERS; for that, see SERVER_EMAIL.
  • EMAIL_BACKEND
Default: 'django.core.mail.backends.smtp.EmailBackend'

The backend to use for sending emails. For the list of available backends see Sending email.
  • EMAIL_FILE_PATH
Default: Not defined

The directory used by the file email backend to store output files.
  • EMAIL_HOST
Default: 'localhost'

The host to use for sending email.

See also EMAIL_PORT
  • EMAIL_PORT
Default: 25

Port to use for the SMTP server defined in EMAIL_HOST
  • EMAIL_USE_LOCALTIME
Default: False

Whether to send the SMTP Date header of email messages in the local time zone (True) or in UTC (False).
  • EMAIL_SSL_CERTFILE
Default: None

If EMAIL_USE_SSL or EMAIL_USE_TLS is True, you can optionally specify the path to a PEM-formatted certificate chain file to use for the SSL connection.
  • EMAIL_SSL_KEYFILE
Default: None

If EMAIL_USE_SSL or EMAIL_USE_TLS is True, you can optionally specify the path to a PEM-formatted private key file to use for the SSL connection.

Note that setting EMAIL_SSL_CERTFILE and EMAIL_SSL_KEYFILE doesn’t result in any certificate checking. They’re passed to the underlying SSL connection. Please refer to the documentation of Python’s ssl.wrap_socket() function for details on how the certificate chain file and private key file are handled.
  • EMAIL_TIMEOUT
Default: None

Specifies a timeout in seconds for blocking operations like the connection attempt.
  • EMAIL_SUBJECT_PREFIX
Default: '[Django] '

Subject-line prefix for email messages sent with django.core.mail.mail_admins or django.core.mail.mail_managers. You’ll probably want to include the trailing space.
  • EMAIL_USE_TLS
Default: False

Whether to use a TLS (secure) connection when talking to the SMTP server. This is used for explicit TLS connections, generally on port 587. If you are experiencing hanging connections, see the implicit TLS setting EMAIL_USE_SSL
posted @ 2020-05-24 18:29  Jenvid  阅读(461)  评论(0编辑  收藏  举报