Просмотр исходного кода

增加异常日志收集功能

linyk 2 лет назад
Родитель
Сommit
d7e363ae75

+ 7 - 0
core/src/main/java/com/mooctest/crowd/domain/dao/ExceptionLogDao.java

@@ -0,0 +1,7 @@
+package com.mooctest.crowd.domain.dao;
+
+import com.mooctest.crowd.domain.model.ExceptionLogPO;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+public interface ExceptionLogDao extends JpaRepository<ExceptionLogPO, Long> {
+}

+ 14 - 0
core/src/main/java/com/mooctest/crowd/domain/domainobject/ExceptionLog.java

@@ -0,0 +1,14 @@
+package com.mooctest.crowd.domain.domainobject;
+
+import lombok.*;
+
+import java.sql.Timestamp;
+
+@Data
+public class ExceptionLog {
+    private String id;
+    private String uri;
+    private String params;
+    private String exception;
+    private Timestamp addTime;
+}

+ 24 - 0
core/src/main/java/com/mooctest/crowd/domain/model/ExceptionLogPO.java

@@ -0,0 +1,24 @@
+package com.mooctest.crowd.domain.model;
+
+import lombok.*;
+
+import javax.persistence.*;
+import java.sql.Timestamp;
+
+@Data
+@Entity
+@Table(name = "exception_log")
+public class ExceptionLogPO {
+    @Id
+    @Column(name = "id")
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+    @Column(name = "exception", columnDefinition = "text", nullable = false)
+    private String exception;
+    @Column(name = "uri", length = 50, nullable = true)
+    private String uri;
+    @Column(name = "params", length = 500, nullable = true)
+    private String params;
+    @Column(name = "add_time", nullable = false)
+    private Timestamp addTime;
+}

+ 21 - 0
core/src/main/java/com/mooctest/crowd/domain/repository/ExceptionLogRepository.java

@@ -0,0 +1,21 @@
+package com.mooctest.crowd.domain.repository;
+
+import com.mooctest.crowd.domain.dao.ExceptionLogDao;
+import com.mooctest.crowd.domain.domainobject.ExceptionLog;
+import com.mooctest.crowd.domain.model.ExceptionLogPO;
+import com.mooctest.crowd.domain.util.Converter;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+@Component
+public class ExceptionLogRepository implements IExceptionLogRepository {
+    @Autowired
+    private ExceptionLogDao exceptionLogDao;
+
+    @Override
+    public void save(ExceptionLog exceptionLog) {
+        ExceptionLogPO exceptionLogPO = Converter.convert(ExceptionLogPO.class, exceptionLog);
+        exceptionLogDao.save(exceptionLogPO);
+    }
+}

+ 8 - 0
core/src/main/java/com/mooctest/crowd/domain/repository/IExceptionLogRepository.java

@@ -0,0 +1,8 @@
+package com.mooctest.crowd.domain.repository;
+
+
+import com.mooctest.crowd.domain.domainobject.ExceptionLog;
+
+public interface IExceptionLogRepository {
+    void save(ExceptionLog exceptionLogDO);
+}

+ 49 - 5
site/src/main/java/com/mooctest/crowd/site/controller/advice/ExceptionAdvice.java

@@ -1,14 +1,24 @@
 package com.mooctest.crowd.site.controller.advice;
 
+import com.mooctest.crowd.domain.domainobject.ExceptionLog;
 import com.mooctest.crowd.domain.exception.*;
 import com.mooctest.crowd.site.constants.ResponseConstant;
+import com.mooctest.crowd.site.service.ExceptionLogService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Enumeration;
 
 /**
  * @author: Diors.Po
@@ -18,11 +28,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
 @Slf4j
 @RestControllerAdvice
 public class ExceptionAdvice {
+    @Autowired
+    private ExceptionLogService exceptionLogService;
 
     @ExceptionHandler(BaseException.class)
     @ResponseStatus(HttpStatus.BAD_REQUEST)
     public String handleException(Exception e){
-        log.error("访问出错:"+e.getMessage(), e);
+        logException(e);
         if (e instanceof AccountNotExistException){
             return ResponseConstant.USER_NOT_EXISTS;
         } else if (e instanceof NullPointerException){
@@ -63,14 +75,14 @@ public class ExceptionAdvice {
     @ExceptionHandler(UnauthorizedException.class)
     @ResponseStatus(HttpStatus.UNAUTHORIZED)
     public String handleUnauthorized(UnauthorizedException e){
-        log.info("401:未经认证的请求");
+        logException(e);
         return e.getMessage();
     }
 
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
     @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
     public String handlMethodNotAllowdd(HttpRequestMethodNotSupportedException e){
-        log.info("method not allowdd", e);
+        logException(e);
         return e.getMessage();
     }
 
@@ -78,7 +90,7 @@ public class ExceptionAdvice {
     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
     @ResponseBody
     public String handleSystemException(Exception e){
-        log.error("System Error: "+e.getMessage(), e);
+        logException(e);
 //        return "系统异常 " + e.getMessage();
         return e.getMessage();
     }
@@ -86,7 +98,39 @@ public class ExceptionAdvice {
     @ExceptionHandler(Excel2ProjectException.class)
     @ResponseStatus(HttpStatus.BAD_REQUEST)
     public String handleExcel2ProjectException(Excel2ProjectException e){
-        log.error("Excel表中存在错误:"+e.getErrorLogs());
+        logException(e);
         return "Excel表中存在错误:"+ e.getErrorLogs();
     }
+
+    public void logException(Exception e) {
+        e.printStackTrace();
+        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+        HttpServletRequest request = requestAttributes.getRequest();
+        String exception = ExceptionUtils.getStackTrace(e);
+        String uri = request.getRequestURI();
+        Enumeration<String> paramNames = request.getParameterNames();
+        StringBuilder paramSb = new StringBuilder();
+        while(paramNames.hasMoreElements()) {
+            String paramName = paramNames.nextElement();
+            String[] paramVals = request.getParameterValues(paramName);
+            if (paramName.equals("password")) {
+                paramVals[0] = "******";
+            }
+            String paramValues = StringUtils.joinWith("$$", paramVals);
+            paramSb.append(paramName).append(paramValues).append("&");
+        }
+        String params = "";
+        if (paramSb.length() > 1) {
+            params = paramSb.substring(0, paramSb.length() - 1);
+        }
+        ExceptionLog exceptionLog = new ExceptionLog();
+        exceptionLog.setException(exception);
+        exceptionLog.setUri(uri);
+        exceptionLog.setParams(params);
+        try {
+            exceptionLogService.save(exceptionLog);
+        } catch (Exception e1) {
+            e1.printStackTrace();
+        }
+    }
 }

+ 7 - 0
site/src/main/java/com/mooctest/crowd/site/service/ExceptionLogService.java

@@ -0,0 +1,7 @@
+package com.mooctest.crowd.site.service;
+
+import com.mooctest.crowd.domain.domainobject.ExceptionLog;
+
+public interface ExceptionLogService {
+    public void save(ExceptionLog exceptionLog);
+}

+ 20 - 0
site/src/main/java/com/mooctest/crowd/site/service/impl/ExceptionLogServiceImpl.java

@@ -0,0 +1,20 @@
+package com.mooctest.crowd.site.service.impl;
+
+import com.mooctest.crowd.domain.domainobject.ExceptionLog;
+import com.mooctest.crowd.domain.repository.ExceptionLogRepository;
+import com.mooctest.crowd.site.service.ExceptionLogService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.sql.Timestamp;
+
+@Service
+public class ExceptionLogServiceImpl implements ExceptionLogService {
+    @Autowired
+    private ExceptionLogRepository exceptionLogRepository;
+
+    public void save(ExceptionLog exceptionLog) {
+        exceptionLog.setAddTime(new Timestamp(System.currentTimeMillis()));
+        exceptionLogRepository.save(exceptionLog);
+    }
+}