django中邮件日志的配置
Posted louyifei0824
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django中邮件日志的配置相关的知识,希望对你有一定的参考价值。
邮件的发送及配置
# 配置邮件 EMAIL_USE_SSL = True EMAIL_HOST = ‘smtp.qq.com‘ # 如果是 163 改成 smtp.163.com EMAIL_PORT = 465 EMAIL_HOST_USER = "[email protected]" # 帐号 EMAIL_HOST_PASSWORD = "xxx" # 授权码(****) DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
import hashlib import uuid def get_unique_str(): # 拿到uuid4的字符串 uuid_str = str(uuid.uuid4()).encode("utf-8") # 使用md5摘要 md5 = hashlib.md5() md5.update(uuid_str) # 返回十六进制的字符 return md5.hexdigest() def send_verify_mail(req): title = "来自xx的问候" msg = "" from_email = settings.DEFAULT_FROM_EMAIL recieve = [ "[email protected]" ] code = get_unique_str() # 拼接URL url = "http://" + req.get_host() + "/myapp/verify/"+ code print(url) # 渲染html页面 template = get_template("verify.html") html = template.render({"url": url}) # print(html) # 发送邮件 send_mail(title, msg, from_email, recieve, html_message=html) # 将发送的结果保存到缓存 user_id = 1 #此处应该是真实的用户id mycache.set(code, user_id, 60 * 60) return HttpResponse("ok") def verify(req, code): # 获取网址后边的路径 字符串 # 去缓存尝试拿数据 val = mycache.get(code) # 如果能拿到 我们就修改is_active字段 if val: MyUser.objects.filter(pk=int(val)).update(is_active=True) mycache.delete(code) return HttpResponse("验证成功") # 如果没拿到 就告诉他链接无效 else: return HttpResponse("链接无效")
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 欢迎注册 <br> <a href="{{ url }}">{{ url }}</a> </body> </html>
日志的配置及输出
ADMINS = ( (‘xxx‘, ‘[email protected]‘), (‘xx‘, ‘[email protected]‘) ) EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend‘ SERVER_EMAIL = EMAIL_HOST_USER #邮箱账号 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‘ }, ‘easy‘: { ‘format‘: ‘%(asctime)s|%(lineno)d|%(message)s‘ } }, ‘filters‘: { # 过滤条件 ‘require_debug_false‘: { ‘()‘: ‘django.utils.log.RequireDebugFalse‘, }, ‘require_debug_true‘: { ‘()‘: ‘django.utils.log.RequireDebugTrue‘ } }, ‘handlers‘: { ‘null‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.NullHandler‘, }, ‘mail_admins‘: { ‘level‘: ‘ERROR‘, ‘class‘: ‘django.utils.log.AdminEmailHandler‘, ‘filters‘: [‘require_debug_false‘], }, ‘debug‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.handlers.RotatingFileHandler‘, ‘filename‘: os.path.join(BASE_DIR, "log", ‘debug.log‘), # 文件路径 ‘maxBytes‘: 1024 * 1024 * 5, #5M的日志 ‘backupCount‘: 15, #允许有多少个日志文件 ‘formatter‘: ‘easy‘, }, ‘console‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.StreamHandler‘, ‘formatter‘: ‘standard‘, }, }, ‘loggers‘: { ‘django‘: { ‘handlers‘: [‘debug‘], ‘level‘: ‘DEBUG‘, ‘propagate‘: False }, ‘django.request‘: { #系统自动调用 ‘handlers‘: [‘debug‘, ‘mail_admins‘], ‘level‘: ‘ERROR‘, ‘propagate‘: True, # 是否继承父类的log信息 }, # 对于不在 ALLOWED_HOSTS 中的请求不发送报错邮件 ‘django.security.DisallowedHost‘: { ‘handlers‘: [‘null‘], ‘propagate‘: False, }, } }
使用:
from django.http import HttpResponse import logging log = logging.getLogger(‘django‘) def test(req): log.info("我是info") 1/0 return HttpResponse("ok")
以上是关于django中邮件日志的配置的主要内容,如果未能解决你的问题,请参考以下文章