新功能: 完善日志信息

This commit is contained in:
李强
2023-01-24 20:43:15 +08:00
parent f75c444699
commit 4804df5079
6 changed files with 158 additions and 59 deletions

View File

@@ -93,7 +93,6 @@ TEMPLATES = [
WSGI_APPLICATION = "application.wsgi.application"
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
@@ -190,80 +189,67 @@ CHANNEL_LAYERS = {
# ================================================= #
# log 配置部分BEGIN #
SERVER_LOGS_FILE = os.path.join(BASE_DIR, "logs", "server.log")
ERROR_LOGS_FILE = os.path.join(BASE_DIR, "logs", "error.log")
LOGS_FILE = os.path.join(BASE_DIR, "logs")
if not os.path.exists(os.path.join(BASE_DIR, "logs")):
os.makedirs(os.path.join(BASE_DIR, "logs"))
# 格式:[2020-04-22 23:33:01][micoservice.apps.ready():16] [INFO] 这是一条日志:
# 格式:[日期][模块.函数名称():行号] [级别] 信息
STANDARD_LOG_FORMAT = (
"[%(asctime)s][%(name)s.%(funcName)s():%(lineno)d] [%(levelname)s] %(message)s"
)
CONSOLE_LOG_FORMAT = (
"[%(asctime)s][%(name)s.%(funcName)s():%(lineno)d] [%(levelname)s] %(message)s"
)
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {"format": STANDARD_LOG_FORMAT},
"console": {
"format": CONSOLE_LOG_FORMAT,
"servers": {
"format": "%(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
},
"file": {
"format": CONSOLE_LOG_FORMAT,
"datefmt": "%Y-%m-%d %H:%M:%S",
},
}
},
"handlers": {
"file": {
"level": "INFO",
"class": "logging.handlers.RotatingFileHandler",
"filename": SERVER_LOGS_FILE,
"servers": {
"logging_levels": ["info", "error", "warning"],
"class": "dvadmin.utils.log.InterceptTimedRotatingFileHandler", # 这个路径看你本地放在哪里(下面的log文件)
"filename": os.path.join(LOGS_FILE, "server.log"),
"when": "D",
"interval": 1,
"maxBytes": 1024 * 1024 * 100, # 100 MB
"backupCount": 5, # 最多备份5个
"formatter": "standard",
"backupCount": 1,
"formatter": "servers",
"encoding": "utf-8",
},
"error": {
"level": "ERROR",
"class": "logging.handlers.RotatingFileHandler",
"filename": ERROR_LOGS_FILE,
"maxBytes": 1024 * 1024 * 100, # 100 MB
"backupCount": 3, # 最多备份3个
"formatter": "standard",
"encoding": "utf-8",
},
"console": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "console",
},
}
},
"loggers": {
# default日志
"": {
"handlers": ["console", "error", "file"],
"level": "INFO",
'django': {
'handlers': ['servers'],
'propagate': False,
'level': "INFO"
},
"django": {
"handlers": ["console", "error", "file"],
"level": "INFO",
"propagate": False,
'': {
'handlers': ['servers'],
'propagate': False,
'level': "ERROR"
},
"scripts": {
"handlers": ["console", "error", "file"],
"level": "INFO",
"propagate": False,
'celery': {
'handlers': ['servers'],
'propagate': False,
'level': "INFO"
},
# 数据库相关日志
"django.db.backends": {
"handlers": [],
"propagate": True,
'django.db.backends': {
'handlers': ['servers'],
'propagate': False,
'level': "INFO"
},
'django.request': {
'handlers': ['servers'],
'propagate': False,
'level': "DEBUG"
},
"uvicorn.error": {
"level": "INFO",
"handlers": ["servers"],
},
"uvicorn.access": {
"handlers": ["servers"],
"level": "INFO",
"propagate": False
},
},
}