import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
if not os.path.exists(BASE_LOG_DIR):
os.makedirs(BASE_LOG_DIR)
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'default': {
'format': '[%(levelname)s] [%(name)s] %(asctime)s %(module)s %(process)d %(thread)d %(message)s'}
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
},
'handlers': {
'mail_admin': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler',
'include_html': False,
},
'error': {
'level':'ERROR',
'class':'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_LOG_DIR, 'error.log'),
'maxBytes':1024 * 1024 * 1,
'backupCount': 5,
'formatter':'default',
},
'warn': {
'level': 'WARNING',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_LOG_DIR, 'warn.log'),
'maxBytes': 1024 * 1024 * 1,
'backupCount': 5,
'formatter': 'default',
},
},
'loggers': {
'django': {
'handlers': ['error', 'warn'],
'level': 'DEBUG',
'propagate': False
},
'django.request': {
'handlers': ['mail_admin', 'error', 'warn'],
'level': 'DEBUG',
'propagate': False
}
}
}
重定义django和django.request两个内置记录器,配置django记录器把接收到的所有警告级别以上的消息输出到warn.log和error.log文件中,配置django.request记录器把警告级别以上消息输出到本地文件,DEBUG=False时错误级别以上消息发送邮件
然后setting.py中要配置好邮件发送接收方,服务主机(注:发送方的SMTP服务要打开)