Prechádzať zdrojové kódy

新增: 云飞监控统一日志记录功能

root 3 rokov pred
rodič
commit
121607f71a

+ 2 - 0
monitor/monitor/middleware.py

@@ -1,5 +1,6 @@
 # coding:utf-8
 
+from loguru import logger
 from django.http.response import JsonResponse
 from django.utils.deprecation import MiddlewareMixin
 
@@ -56,4 +57,5 @@ class ExceptionGlobeMiddleware(MiddlewareMixin):
                 'paging': {}
             }
         }
+        logger.exception(exception)
         return JsonResponse(data=ex_data, status=500)

+ 48 - 4
monitor/monitor/settings.py

@@ -11,9 +11,25 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
 """
 
 import os
+from loguru import logger
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+log_file = os.path.join(BASE_DIR, 'log/django_error.log')
+
+# 一个地方配置后,全局都可以from loguru import logger 导入使用
+logger.add(
+    log_file,
+    level='DEBUG',
+    format='{time:YYYY-MM-DD HH:mm:ss} {level} {file}[line:{line}] {message}',
+    rotation="00:00",
+    retention='10 days',
+    encoding="utf-8",
+    compression='tar.gz',
+    diagnose=True,
+    backtrace=True,
+    enqueue=True
+)
 
 
 # Quick-start development settings - unsuitable for production
@@ -40,12 +56,14 @@ INSTALLED_APPS = [
     'django.contrib.staticfiles',
     'rest_framework',
     'drf_spectacular',
+    'corsheaders',
     'monitor_app'
 ]
 
 MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
+    'corsheaders.middleware.CorsMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -82,7 +100,6 @@ TEMPLATES = [
 
 WSGI_APPLICATION = 'monitor.wsgi.application'
 
-
 # Database
 # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
 
@@ -93,7 +110,6 @@ DATABASES = {
     }
 }
 
-
 # Password validation
 # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
 
@@ -112,7 +128,6 @@ AUTH_PASSWORD_VALIDATORS = [
     },
 ]
 
-
 # Internationalization
 # https://docs.djangoproject.com/en/2.2/topics/i18n/
 
@@ -126,8 +141,37 @@ USE_L10N = True
 
 USE_TZ = True
 
-
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/2.2/howto/static-files/
 
 STATIC_URL = '/static/'
+
+CORS_ALLOW_CREDENTIALS = True
+CORS_ORIGIN_ALLOW_ALL = True
+
+# CORS_ORIGIN_WHITELIST = (
+#     'http://*'
+# )
+
+CORS_ALLOW_METHODS = (
+    'DELETE',
+    'GET',
+    'OPTIONS',
+    'PATCH',
+    'POST',
+    'PUT'
+)
+
+CORS_ALLOW_HEADERS = (
+    'XMLHttpRequest',
+    'X_FILENAME',
+    'accept-encoding',
+    'authorization',
+    'content-type',
+    'dnt',
+    'origin',
+    'user-agent',
+    'x-csrftoken',
+    'x-requested-with',
+    'Pragma',
+)

+ 7 - 3
monitor/monitor_app/process/views.py

@@ -57,7 +57,7 @@ class ProcessDetailView(APIView):
             200: OpenApiResponse(description="无返回值")
         }
     )
-    def put(self, request):
+    def post(self, request, *args, **kwargs):
         serobj = ProcessDetailPutSerializer(data=request.data)
         if not serobj.is_valid():
             return Response(serobj.errors, status=400)
@@ -71,6 +71,10 @@ class ProcessDetailView(APIView):
             sup.stop(name)
         return Response(status=200)
 
+
+class ProcessDeleteView(APIView):
+    permission_classes = [LoginPermission]
+
     @extend_schema(
         description="删除进程进程接口",
         request=ProcessDetailDeleteSerializer,
@@ -78,7 +82,7 @@ class ProcessDetailView(APIView):
             200: OpenApiResponse(description="无返回值")
         }
     )
-    def delete(self, request):
+    def post(self, request, *args, **kwargs):
         serobj = ProcessDetailDeleteSerializer(data=request.data)
         if not serobj.is_valid():
             return Response(serobj.errors, status=400)
@@ -86,4 +90,4 @@ class ProcessDetailView(APIView):
         name = request.data['name']
         sup = SupervisordUtils()
         sup.remove(name)
-        return Response(status=200)
+        return Response(status=200)

+ 3 - 2
monitor/monitor_app/urls.py

@@ -1,7 +1,7 @@
 # coding:utf-8
 
 from django.urls import path, re_path
-from .process.views import ProcessListView, ProcessDetailView
+from .process.views import ProcessListView, ProcessDetailView, ProcessDeleteView
 from .system.views import SystemDetailView
 from .user.views import UserLoginView, UserLogoutView
 
@@ -10,5 +10,6 @@ urlpatterns = [
     re_path('user/logout/?', UserLogoutView.as_view()),
     re_path('process/?', ProcessListView.as_view()),
     re_path('process/detail/?', ProcessDetailView.as_view()),
-    re_path('system/detail/?', SystemDetailView.as_view())
+    re_path('process/delete/?', ProcessDeleteView.as_view()),
+    re_path('system/detail/?', SystemDetailView.as_view()),
 ]