zoukankan      html  css  js  c++  java
  • django 全局日志/以QQ邮箱为例发送系统错误邮件

     1.注意事项

                   发送邮件已QQ为例

                   如何开启QQ邮箱smtp 请参照

                   https://service.mail.qq.com/cgi-bin/help?subtype=1&no=166&id=28

     settings.py 配置如下

    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = False # False的时候才会发送邮件提醒
    
    ALLOWED_HOSTS = ["*"]
    
    # Application definition
    
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    
    # Database
    # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
    
    # Password validation
    # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
    # Internationalization
    # https://docs.djangoproject.com/en/3.1/topics/i18n/
    
    LANGUAGE_CODE = 'zh-hans'  # 设置为中文
    TIME_ZONE = 'Asia/Shanghai'  # 设置时区
    USE_I18N = True  # 默认为True,是否启用自动翻译系统
    USE_L10N = True  # 默认False,以本地化格式显示数字和时间
    USE_TZ = False  # 默认值True。若使用了本地时间,必须设为False
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/3.1/howto/static-files/
    
    
    
    STATIC_URL = '/static/'
    STATIC_ROOT = 'static'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "/static/")
    ]
    
    # 管理员邮箱
    ADMINS = (
        ('wangcongxing', '2256807897@qq.com'),
    )
    
    # 非空链接,却发生404错误,发送通知MANAGERS
    SEND_BROKEN_LINK_EMAILS = True
    MANAGERS = ADMINS
    
    # Email设置
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.qq.com'  # QQ邮箱SMTP服务器(邮箱需要开通SMTP服务)
    EMAIL_PORT = 25  # QQ邮箱SMTP服务端口
    EMAIL_HOST_USER = '3136143551@qq.com'  # 我的邮箱帐号
    EMAIL_HOST_PASSWORD = '*************************'  # 授权码  不是你的QQ密码
    EMAIL_SUBJECT_PREFIX = 'website'  # 为邮件标题的前缀,默认是'[django]'
    EMAIL_USE_TLS = True  # 开启安全链接
    DEFAULT_FROM_EMAIL = SERVER_EMAIL = EMAIL_HOST_USER  # 设置发件人
    
    
    log_path = os.path.join(BASE_DIR, "logs")
    if not os.path.exists(log_path):
        os.makedirs("logs")
    
    # logging日志配置
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {  # 日志格式
            'standard': {
                'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'}
        },
        'filters': {  # 过滤器
            'require_debug_false': {
                '()': 'django.utils.log.RequireDebugFalse',
            }
        },
        'handlers': {  # 处理器
            'null': {
                'level': 'DEBUG',
                'class': 'logging.NullHandler',
            },
            'mail_admins': {  # 发送邮件通知管理员
                'level': 'ERROR',
                'class': 'django.utils.log.AdminEmailHandler',
                'filters': ['require_debug_false'],  # 仅当 DEBUG = False 时才发送邮件
                'include_html': True,
            },
            'debug': {  # 记录到日志文件(需要创建对应的目录,否则会出错)
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, "logs", 'debug.log'),  # 日志输出文件
                'maxBytes': 1024 * 1024 * 5,  # 文件大小
                'backupCount': 5,  # 备份份数
                'formatter': 'standard',  # 使用哪种formatters日志格式
            },
            'console': {  # 输出到控制台
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
            },
        },
        'loggers': {  # logging管理器
            'django': {
                'handlers': [],#如果需要在控制台输出['console']
                'level': 'DEBUG',
                'propagate': False
            },
            'django.request': {
                'handlers': ['debug', 'mail_admins'],
                'level': 'ERROR',
                'propagate': True,
            },
            # 对于不在 ALLOWED_HOSTS 中的请求不发送报错邮件
            'django.security.DisallowedHost': {
                'handlers': ['null'],
                'propagate': False,
            },
        }
    }

    2.浏览器输入错误请求

    3.运行效果

  • 相关阅读:
    AX 2012 Security Framework
    The new concept 'Model' in AX 2012
    How to debug the SSRS report in AX 2012
    Using The 'Report Data Provider' As The Data Source For AX 2012 SSRS Report
    Deploy SSRS Report In AX 2012
    AX 2012 SSRS Report Data Source Type
    《Taurus Database: How to be Fast, Available, and Frugal in the Cloud》阅读笔记
    图分析理论 大纲小结
    一文快速了解Posix IO 缓冲
    #转载备忘# Linux程序调试工具
  • 原文地址:https://www.cnblogs.com/wangcongxing/p/13652119.html
Copyright © 2011-2022 走看看