增加了log基本配置

This commit is contained in:
ladeng07
2022-05-06 16:19:42 +08:00
parent 51552904f9
commit f91991662b
2 changed files with 64 additions and 14 deletions
+64 -14
View File
@@ -1,35 +1,25 @@
"""
Django settings for ITShowPlatform project.
Generated by 'django-admin startproject' using Django 4.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
from pathlib import Path
import os
import configparser
import logging
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
conf = configparser.RawConfigParser()
conf.read(str(BASE_DIR)+r"\config.ini",encoding="utf-8")
conf.read(os.path.join(BASE_DIR, "config.ini"),encoding="utf-8")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-0cn#v4ei2(^n+txyh4%3d5sllz6mknz#7t$!cq-d!ly*_rwvh2'
SECRET_KEY = conf.get("Django","SECRET_KEY"),
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = False
ALLOWED_HOSTS = ["*"]
@@ -155,3 +145,63 @@ EMAIL_HOST_PASSWORD = conf.get("email","EMAIL_HOST_PASSWORD") # 密码 (注意
EMAIL_USE_SSL = conf.get("email","EMAIL_USE_SSL") # 一般都为False
EMAIL_FROM = conf.get("email","EMAIL_FROM") # 邮箱来自
ADMINS = (
('ladeng','2312936963@qq.com'),
)
MANAGERS = ADMINS
# 创建log文件的文件夹
LOG_DIR = os.path.join(BASE_DIR, "logs")
if not os.path.exists(LOG_DIR):
os.mkdir(LOG_DIR)
# 基本配置,可以复用的
LOGGING = {
"version": 1,
"disable_existing_loggers": False, # 禁用已经存在的logger实例
"filters": {"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}},
"formatters": { # 定义了两种日志格式
"verbose": { # 详细
"format": "%(levelname)s %(asctime)s %(module)s "
"%(process)d %(thread)d %(message)s"
},
'simple': { # 简单
'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
},
},
"handlers": { # 定义了三种日志处理方式
"mail_admins": { # 只有debug=False且Error级别以上发邮件给admin
"level": "ERROR",
"filters": ["require_debug_false"],
"class": "django.utils.log.AdminEmailHandler",
},
'file': { # 对INFO级别以上信息以日志文件形式保存
'level': "INFO",
'class': 'logging.handlers.RotatingFileHandler', # 滚动生成日志,切割
'filename': os.path.join(LOG_DIR,'django.log'), # 日志文件名
'maxBytes': 1024 * 1024 * 10, # 单个日志文件最大为10M
'backupCount': 5, # 日志备份文件最大数量
'formatter': 'simple', # 简单格式
'encoding': 'utf-8', # 放置中文乱码
},
"console": { # 打印到终端console
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
},
},
"root": {"level": "INFO", "handlers": ["console"]},
"loggers": {
"django.request": { # Django的request发生error会自动记录
"handlers": ["mail_admins"],
"level": "ERROR",
"propagate": True, # 向不向更高级别的logger传递
},
"django.security.DisallowedHost": { # 对于不在 ALLOWED_HOSTS 中的请求不发送报错邮件
"level": "ERROR",
"handlers": ["console", "mail_admins"],
"propagate": True,
},
},
}
View File