32 项目结构 & 事务 & Logging日志
1 项目结构
以下主要是以drf编写api时的结构为示例。
1.1 APP结构
1.1.1 单APP
例如:订单系统
1.1.2 Base + 业务APP
例如:供应链系统
1.1.3 独立的APP
app中的功能各自独立的,每个app中编写自己的 models / views
等。
1.2 视图结构
1.2.1 view.py
1.2.2 views + 文件
根据业务拆分成多个视图文件,每个视图文件中实现指定的一些业务。
1.2.3 views + 文件夹
这里的router路由需要注意:
1.3 返回值
1.3.1 自定义mixins
将视图中的 mixins
相关的视图重写,业务视图继承自己视图,对于各种情况的返回值进行定制:
1.内置返回
对于认证、权限等组件进行自定义,实现返回自定义的格式数据。
2.视图返回
对于用户请求的返回值进行定制。
1.3.2 自定义异常处理机制
在之前学习源码时,我们知道:请求到来都会执行dispatch方法,在 try...except
代码块中:
- 触发:认证、权限、限流等
- 执行视图
- 上述过程如果出现异常,则由
self.handle_exception(exc)
对异常处理并封装返回值,然后返回。
1.handle_exception源码
2.自定义exception_handler
详细案例见:dbhot11.zip
utils/handlers.py
from django.http import Http404
from rest_framework import exceptions
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import Throttled
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import NotAuthenticated
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.views import set_rollback
def exception_handler(exc, context):
if isinstance(exc, Http404):
exc = exceptions.NotFound()
exc.ret_code = 1001
elif isinstance(exc, PermissionDenied):
exc = exceptions.PermissionDenied()
exc.ret_code = 1002
elif isinstance(exc, (AuthenticationFailed, NotAuthenticated)):
exc.ret_code = 1003
elif isinstance(exc, Throttled):
exc.ret_code = 1004
elif isinstance(exc, ValidationError):
exc.ret_code = 1005
# 只处理drf相关的异常
if isinstance(exc, exceptions.APIException):
headers = {}
if getattr(exc, 'auth_header', None):
headers['WWW-Authenticate'] = exc.auth_header
if getattr(exc, 'wait', None):
headers['Retry-After'] = '%d' % exc.wait
if isinstance(exc.detail, (list, dict)):
data = exc.detail
else:
code = getattr(exc, 'ret_code', None) or -1
data = {'code': code, 'detail': exc.detail}
set_rollback()
return Response(data, status=exc.status_code, headers=headers)
return None
utils/exceptions.py
from rest_framework import exceptions
class ExtraException(exceptions.APIException):
def __init__(self, detail=None, ret_code=None, code=None):
super().__init__(detail, code)
self.ret_code = ret_code
views.py
from rest_framework import exceptions
from rest_framework import serializers
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin
from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission
from rest_framework.throttling import BaseThrottle
from api import models
from utils.exceptions import ExtraException
class ExtraAuthentication(BaseAuthentication):
def authenticate(self, request):
raise exceptions.AuthenticationFailed("认证失败")
def authenticate_header(self, request):
return "api"
class ExtraPermission(BasePermission):
def has_permission(self, request, view):
return False
def has_object_permission(self, request, view, obj):
return False
class ExtraThrottle(BaseThrottle):
def allow_request(self, request, view):
return False
class DemoSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserInfo
fields = "__all__"
class DemoView(ListModelMixin, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin,
GenericViewSet):
# authentication_classes = [ExtraAuthentication]
# throttle_classes = [ExtraThrottle]
# permission_classes = [ExtraPermission]
queryset = models.UserInfo.objects.all()
serializer_class = DemoSerializer
def perform_create(self, serializer):
self.dispatch
if True:
# 自定义错误
# raise ExtraException("数据异常")
raise ExtraException("更新失败", ret_code=9000)
serializer.save()
def finalize_response(self, request, response, *args, **kwargs):
response = super().finalize_response(request, response, *args, **kwargs)
if response.exception:
return response
response.data = {'code': 0, 'data': response.data}
return response
3.正常返回
def finalize_response(self, request, response, *args, **kwargs):
response = super().finalize_response(request, response, *args, **kwargs)
if response.exception:
return response
response.data = {'code': 0, 'data': response.data}
return response
可以通过自定义 DrfGenericViewSet
:
from rest_framework.viewsets import GenericViewSet as DrfGenericViewSet
class GenericViewSet(DrfGenericViewSet):
def finalize_response(self, request, response, *args, **kwargs):
response = super().finalize_response(request, response, *args, **kwargs)
if response.exception:
return response
response.data = {'code': 0, 'data': response.data}
return response
可以定义到中间件中使用:
from django.utils.deprecation import MiddlewareMixin
class ReturnCodeMiddleware(MiddlewareMixin):
def process_response(self, request, response):
if not hasattr(response, 'exception'):
return response
if response.exception:
return response
response.data = {'code': 0, 'data': response.data}
response._is_rendered = False
response.content = response.render().content
return response
2 事务
2.1 局部事务(*)
基于上下文管理,如果出现异常则自动回滚;无异常则自动提交。
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db import transaction
from api import models
class Demo1View(APIView):
def get(self, request, *args, **kwargs):
try:
with transaction.atomic():
models.UserInfo.objects.create(name='v1', age=1)
models.Order.objects.create(name='v1', age=1)
except Exception as e:
print("异常,自动回滚")
return Response("...")
事务提交的回调函数(本质上就是事务完成后,自动执行一个函数):
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db import transaction
from api import models
from functools import partial
def db_success_callback(*args, **kwargs):
print(args, **kwargs)
class Demo1View(APIView):
def get(self, request, *args, **kwargs):
try:
with transaction.atomic():
# 回调函数,事务正常提交自动执行
transaction.on_commit(db_success_callback)
transaction.on_commit( partial(db_success_callback, 11, 22, 33) )
models.UserInfo.objects.create(name='v1', age=1)
models.Order.objects.create(title='v1', count=1)
except Exception as e:
print("异常,自动回滚") # on_commit回调函数内部异常时不会回滚
return Response("...")
回滚到 指定事务点:
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db import transaction
from api import models
class Demo1View(APIView):
def get(self, request, *args, **kwargs):
try:
with transaction.atomic():
# 回调函数,事务正常提交自动执行
n1 = transaction.savepoint()
models.UserInfo.objects.create(name='v1', age=1)
n2 = transaction.savepoint()
models.UserInfo.objects.create(name='v2', age=1)
# 必须在事务里面,回顾到指定 事务点,后续东西不提交
transaction.savepoint_rollback(n2)
except Exception as e:
print("异常,自动回滚", e) # on_commit回调函数内部异常时不会回滚
return Response("...")
2.2 视图事务
针对整个视图进行开启事务:
- 视图内,有数据库操作异常,自动回滚
- 视图内,有其他异常,不会回滚。
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db import transaction, IntegrityError
from api import models
class Demo1View(APIView):
@transaction.atomic
def get(self, request, *args, **kwargs):
try:
models.UserInfo.objects.create(name='v100', age=1)
models.UserInfo.objects.create(name="v200", age="xxx") # 有异常,回滚,即:v100不会保存
int("asdf") # 有异常,不会滚,即:两条数据正常保存到数据库
except Exception as e:
pass
return Response("...")
定义事务点,自定义回滚位置:
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db import transaction, IntegrityError
from api import models
class Demo1View(APIView):
@transaction.atomic
def get(self, request, *args, **kwargs):
try:
models.UserInfo.objects.create(name='v10', age=1)
n1 = transaction.savepoint()
models.UserInfo.objects.create(name="v11", age=1)
n2 = transaction.savepoint()
models.UserInfo.objects.create(name='v12', age=1)
n3 = transaction.savepoint()
models.UserInfo.objects.create(name='v13', age=1)
# 后续读取到某些值后,发现 v12不应该创建,那么就可以主动回滚(只生成n2之前的)
transaction.savepoint_rollback(n2)
except Exception as e:
print("有异常", e)
return Response("...")
2.3 全局事务
效率低:项目中一般不会使用。
如果想要开启全局事务,需要在连接数据库时多设置一个参数:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbhot4',
'USER': 'root',
'PASSWORD': 'root123',
'HOST': '127.0.0.1',
'PORT': '3306',
'ATOMIC_REQUESTS': True
}
}
-
只要视图函数执行异常,无论是什么原因触发,均自动回滚。
class Demo1View(APIView): def get(self, request, *args, **kwargs): models.UserInfo.objects.create(name='v1', age=1) models.UserInfo.objects.create(xxxxxxx='v2', age=1) # 错误 return Response("...")
class Demo1View(APIView): def get(self, request, *args, **kwargs): models.UserInfo.objects.create(name='v1', age=1) models.UserInfo.objects.create(name='v2', age=1) int("asdf") # 错误 return Response("...")
-
如果视图函数执行不报错(try处理异常,也叫不报错),则不会回滚
class Demo1View(APIView): def get(self, request, *args, **kwargs): try: models.UserInfo.objects.create(name='v1', age=1) models.UserInfo.objects.create(xxxxxxx='v2', age=1) int("xxx") except Exception as e: pass return Response("...") # 视图函数执行没有报错,不会滚回。
如果开启了全局事务,想要免除某个指定的函数不需要开启事务,则可以使用:
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db import transaction, IntegrityError
from api import models
from django.utils.decorators import method_decorator
@method_decorator(transaction.non_atomic_requests, name='dispatch')
class Demo1View(APIView):
def get(self, request, *args, **kwargs):
models.UserInfo.objects.create(name='v100', age=1)
models.UserInfo.objects.create(name="v200", age="xxx") # 报错
return Response("...")
3 Logging日志
什么时候用到日志?
- 可预知的情况,写日志
- 不可预知情况,写日志
3.1 基础版
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
import logging
# 1. 对日志进行配置
logging.basicConfig(
filename='error.log', # 日志文件
format='%(asctime)s : %(message)s', # 写日志时,文件的格式。
datefmt='%Y-%m-%d %H:%M:%S %p',
level=20 # 级别,以后只有大于20的级别时,才能真正日志内容写入到文件中。
)
# 2.写日志
"""
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
"""
logging.debug("你好呀") # 10,你好呀
logging.info("中午好") # 10,你好呀
logging.error("你傻呀") # 40,你傻呀
默认这种形式不支持在文件中写入日志:
import logging
import traceback
# 1. 对日志进行配置
logging.basicConfig(
filename='v10.log', # 日志文件
format='%(asctime)s : %(message)s', # 写日志时,文件的格式。
datefmt='%Y-%m-%d %H:%M:%S %p',
level=20 # 级别,以后只有大于20的级别时,才能真正日志内容写入到文件中。
)
# 2. 对日志进行配置(不生效,因为已配置过后生成在内存,不再支持设置)
logging.basicConfig(
filename='v100.log', # 日志文件
format='%(asctime)s : %(message)s', # 写日志时,文件的格式。
datefmt='%Y-%m-%d %H:%M:%S %p',
level=20 # 级别,以后只有大于20的级别时,才能真正日志内容写入到文件中。
)
logging.error("沙雕alex")
3.2 对象版(支持多文件)
-
Formatter,格式化。
-
FileHandler,维护文件,专门用于往文件中写内容。
-
Logger,定义级别,大于这个级别才调用 FileHandler 去写内容。
-
写日志
logger = Logger() logger.error("内容") # 10 "xxx"
示例1:
import logging
# 定义 Formatter
fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s")
# 定义 FileHandler
handler_object = logging.FileHandler('v2.log', 'a', encoding='utf-8')
handler_object.setFormatter(fmt)
# 定义 Logger
logger_object = logging.Logger('s1', level=logging.INFO) # 20
logger_object.addHandler(handler_object)
# ===>写日志<====
logger_object.error("alex是个大sb") # 40>20则写入日志
示例2:
import logging
# 定义 Formatter
fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s")
# 定义 FileHandler
handler_object = logging.FileHandler('v2.log', 'a', encoding='utf-8')
handler_object.setFormatter(fmt)
handler_object2 = logging.FileHandler('v22.log', 'a', encoding='utf-8')
handler_object2.setFormatter(fmt)
# 定义 Logger
logger_object = logging.Logger('s1', level=logging.INFO) # 20
logger_object.addHandler(handler_object)
logger_object.addHandler(handler_object2)
# 写日志
logger_object.error("alex是个大sb")
3.3 配置版
示例1:简单配置
import logging.config
# 1. 定义字典
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": True, # 删除已存在其他日志的Handler
'formatters': {
'standard': {
'format': '{asctime} {levelname} :{message}',
'style': '{',
"datefmt": '%Y-%m-%d %H:%M:%S %p',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler', # 写到终端的输出流中
'formatter': 'standard', # 用到的什么格式
},
'demo': {
"class": 'logging.handlers.RotatingFileHandler', # 写到文件中
'formatter': 'standard',
'filename': 'demo.log', # 日志的文件名
'maxBytes': 1024, # 根据文件大小拆分日志
'backupCount': 30, # 5份文件
"encoding": "utf-8"
}
},
'loggers': {
'nb': {
'handlers': ['console', 'demo'],
'level': "INFO", # >=20 则触发日志
'propagate': False
}
}
}
# 2. 根据自定对logging进行配置
logging.config.dictConfig(LOGGING_CONFIG)
# 3. 写日志
logger_object = logging.getLogger("nb")
logger_object.info('6666666666')
示例2:多文件配置
import logging.config
# 1. 定义字典
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": True, # 删除已存在其他日志的Handler
'formatters': {
'standard': {
'format': '{asctime} {levelname} {threadName} :{message}',
'style': '{',
"datefmt": '%Y-%m-%d %H:%M:%S %p',
},
'simple': {
'format': '%(asctime)s %(levelname)s %(message)s',
'style': '%',
"datefmt": '%Y-%m-%d',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'run': {
# 运行日志,按天自动分割
"class": 'logging.handlers.TimedRotatingFileHandler', # 基于时间
'formatter': 'standard',
'filename': "run.log",
'when': 'D', # 根据天拆分日志
'interval': 1, # 1天
'backupCount': 3, # 保留备份
"encoding": "utf-8"
},
'error': {
# 错误日志,按照文件大小分割
"class": 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': 'error.log',
'maxBytes': 1024 * 1025 * 50, # 根据文件大小拆分日志 50M
'backupCount': 5,
"encoding": "utf-8"
},
},
'loggers': {
'run': {
'handlers': ['run'],
'level': "INFO", # >=20 则触发日志
'propagate': True # 触发父级-->’root'
},
'error': {
'handlers': ['console', 'error'],
'level': "ERROR", # >=20 则触发日志
'propagate': False
}
},
'root': {
'handlers': ['console', ],
'level': 'DEBUG',
'propagate': True
}
}
# 2. 根据自定对logging进行配置
logging.config.dictConfig(LOGGING_CONFIG)
# 3. 写日志
# root = logging.getLogger()
# root.info("测试测试")
# run = logging.getLogger('run')
# run.info("测试测试")
# run = logging.getLogger('error')
# run.info("有信息了")
# run.error("错误了")
示例3:过滤
import logging
class CallbackFilter(logging.Filter):
def __init__(self, callback):
self.callback = callback
def filter(self, record):
if self.callback(record):
return True
return False
class DynamicFilter(logging.Filter):
def filter(self, record):
# record,包含了日志相关的对象 logging.LogRecord
# print(record, type(record))
if not record.msg:
return False
return True
import logging.config
# 1. 定义字典
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": True, # 删除已存在其他日志的Handler
'formatters': {
'standard': {
'format': '{asctime} {levelname} {threadName} :{message}',
'style': '{',
"datefmt": '%Y-%m-%d %H:%M:%S %p',
},
'simple': {
'format': '%(asctime)s %(levelname)s %(message)s',
'style': '%',
"datefmt": '%Y-%m-%d',
},
},
"filters": {
"dy": {
"()": "utils.DynamicFilter"
},
"call": {
"()": "utils.CallbackFilter",
"callback": lambda record: len(record.msg) > 4
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'run': {
# 运行日志,按天自动分割
"class": 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'standard',
'filters': ["dy", 'call'],
'filename': "run.log",
'when': 'D', # 根据天拆分日志
'interval': 1, # 1天
'backupCount': 3, # 保留备份
"encoding": "utf-8"
},
'error': {
# 错误日志,按照文件大小分割
"class": 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': 'error.log',
'maxBytes': 1024 * 1025 * 50, # 根据文件大小拆分日志 50M
'backupCount': 5,
"encoding": "utf-8"
},
},
'loggers': {
'run': {
'handlers': ['run'],
'level': "INFO", # >=20 则触发日志
'propagate': True
},
'error': {
'handlers': ['console', 'error'],
'level': "ERROR", # >=20 则触发日志
'propagate': False
}
},
'root': {
'handlers': ['console', ],
'level': 'DEBUG',
'propagate': True
}
}
# 2. 根据自定对logging进行配置
logging.config.dictConfig(LOGGING_CONFIG)
# 3. 写日志
run = logging.getLogger('run')
run.info("测试测xxxxx")
3.4 Django
###########
# LOGGING #
###########
import os
# BASE_LOG_DIR = BASE_DIR / 'log'
BASE_LOG_DIR = os.path.join(BASE_DIR, 'log')
BASE_LOG_DIR.mkdir(exist_ok=True) # 文件夹不存在则创建,存在不创建
# The callable to use to configure logging
LOGGING_CONFIG = "logging.config.dictConfig"
# Custom logging configuration.
# 1. 定义字典
LOGGING = {
"version": 1,
"disable_existing_loggers": False, # 删除已存在其他日志的Handler
'formatters': {
'standard': {
'format': '{asctime} {levelname} {threadName} :{message}',
'style': '{',
"datefmt": '%Y-%m-%d %H:%M:%S %p',
},
'simple': {
'format': '%(asctime)s %(levelname)s %(message)s',
'style': '%',
"datefmt": '%Y-%m-%d',
},
},
# "filters": {
# "dy": {
# "()": "django.utils.log.RequireDebugFalse"
# },
# "call": {
# "()": "django.utils.log.CallbackFilter",
# "callback": lambda record: len(record.msg) > 4
# }
# },
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'run': {
# 运行日志,按天自动分割
"class": 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'standard',
# 'filters': ["dy", 'call'],
'filename': os.path.join(BASE_LOG_DIR, 'run.log'),
'when': 'D', # 根据天拆分日志
'interval': 1, # 1天
'backupCount': 3, # 保留备份
"encoding": "utf-8"
},
'error': {
# 错误日志,按照文件大小分割
"class": 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': os.path.join(BASE_LOG_DIR, 'error.log'),
'maxBytes': 1024 * 1025 * 50, # 根据文件大小拆分日志 50M
'backupCount': 5,
"encoding": "utf-8"
},
},
'loggers': {
'run': {
'handlers': ['run'],
'level': "INFO", # >=20 则触发日志
'propagate': True
},
'error': {
'handlers': ['console', 'error'],
'level': "ERROR", # >=40 则触发日志
'propagate': False
}
},
'root': {
'handlers': ['console', ],
'level': 'DEBUG',
'propagate': True
}
}
logger = logging.getLogger("error")
logger.error("...")
logger = logging.getLogger("run")
logger.info("...")
logger = logging.getLogger()
logger.info("...")