【Django】配置邮件的日志log-20220803

配置邮件的日志log的前提:

  • 前提1:settings.py的LOGGING要设好
  • 前提2:settings.py的EMAIL配置要设好
  • 前提3:views.py或admin.py调用logger

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    # 'formatters': {
    #     'simple': { # exact format is not important, this is the minimum information
    #         'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
    #     },
    # },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },

        'mail_admins': { # Add Handler for mail_admins for `warning` and above
            # 'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        },
        'file': {
            'class': 'logging.FileHandler',
            'filename': os.path.join(LOG_DIR, 'admin.log'),
        },

    },

    'root': {
        'handlers': ['console', 'file','mail_admins'],
        'level': 'INFO',
    },
}


EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# 我使用的是新浪的,host可以在对应邮箱的设置中找到
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 25
# 你的邮箱账号与密码
EMAIL_HOST_USER = 'ci_s******@163.com'
EMAIL_HOST_PASSWORD = 'M******'
# 由于使用25端口,一般都不使用TLS机密,SSL和TSL只需要设置一个,他们同时为True或False
EMAIL_USE_TLS = False
# 发件人,只有这个变量名可以自己自定义,设置在这里是为了减少每次去写
EMAIL_FROM = 'ci_******@163.com'

views.py

        if user:
            if user.is_active:
                login(request,user)
                logger.info("%s %s login" % (datetime.now(),username))
                send_mail('Subject',
                                'hi',
                                'ci_*******@163.com',
                                ['S*******@wistron.com'],
                                fail_silently=False,
                            )
                print('send_mail')
                return HttpResponseRedirect('/')
            else:
                pass
        else:
        	.......

效果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值