Skip to content

Commit

Permalink
添加完整的请求日志打印的中间件,方便定位问题,生产环境不要使用
Browse files Browse the repository at this point in the history
  • Loading branch information
kagxin committed Sep 29, 2019
1 parent 685a788 commit be5380d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
Empty file.
43 changes: 43 additions & 0 deletions apps/utils/middleware/request_log_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging
from django.utils.deprecation import MiddlewareMixin
import uuid
import time

log = logging.getLogger("request_info_logger")


class RequestLogMiddleware(MiddlewareMixin):
def process_request(self, request):
try:
req_id = uuid.uuid1()
request.req_id = req_id
log.error("\nRequest[{}]==========>"
"\nmethod:{}"
"\npath:{}"
"\nauth:{}"
"\nbody:{}"
"\nquery params:{}"
"\n".format(request.req_id,
request.method,
request.path,
request.META.get('HTTP_AUTHORIZATION', 'no auth'),
request.body if hasattr(request, 'body') else 'no body',
request.META.get('QUERY_STRING'), ))
except Exception as e:
log.error("request info middleware error: %s" % e)

def process_response(self, request, response):
try:
log.info("\nResponse[{}][{}]==========>"
"\npath:{}"
"\nmethod:{}"
"\nstatus:{}"
"\ncontent:{}\n".format(request.req_id,
time.time(),
request.path,
request.method,
response.status_code,
response.content, ))
except Exception as e:
log.error("response info middleware error: %s" % e)
return response
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
djangorestframework==3.10.0
djangorestframework-jwt==1.11.0
django==2.2.4
mongoengine==0.18.2
mysqlclient==1.4.2.post1
requests==2.22.0
redis==3.2.1
Expand Down
19 changes: 15 additions & 4 deletions settings/local.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

"""
create database django_template character set utf8;
"""
Expand Down Expand Up @@ -54,8 +55,8 @@
ACCESS_KEY_SECRET = "****"
END_POINT = "oss-cn-shanghai.aliyuncs.com"
BUCKET_NAME = "****"
ALIYUN_OSS_CNAME = "" # 自定义域名,如果不需要可以不填写
BUCKET_ACL_TYPE = "private" # private, public-read, public-read-write
ALIYUN_OSS_CNAME = "" # 自定义域名,如果不需要可以不填写
BUCKET_ACL_TYPE = "private" # private, public-read, public-read-write
# mediafile将自动上传
# DEFAULT_FILE_STORAGE = 'aliyun_oss2_storage.backends.AliyunMediaStorage'
# staticfile将自动上传
Expand All @@ -66,5 +67,15 @@
# ==================
CELERY_BROKER_URL = 'redis://{}/1'.format(redis_host)
CELERY_RESULT_BACKEND = 'redis://{}/1'.format(redis_host)


MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] + [
'apps.utils.middleware.request_log_middleware.RequestLogMiddleware'
]
11 changes: 0 additions & 11 deletions settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,5 @@
else:
raise Exception("Env[SITE_TYPE] error!")

"""
# if use mongodb
from mongoengine import connect
connect(
db=MONGODB_DATABASES['default']['name'],
username=MONGODB_DATABASES['default']['username'],
password=MONGODB_DATABASES['default']['password'],
host=MONGODB_DATABASES['default']['host'].split(":")[0],
port=int(MONGODB_DATABASES['default']['host'].split(":")[1]),
)
"""


0 comments on commit be5380d

Please sign in to comment.