浙江报告与日志功能

This commit is contained in:
2025-04-11 11:03:16 +08:00
parent 6727dee61e
commit 6f890daad6
50 changed files with 2703 additions and 544 deletions

View File

@@ -0,0 +1,105 @@
package com.njcn.gather.advice;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.gather.system.log.service.ISysLogAuditService;
import com.njcn.web.utils.HttpServletUtil;
import com.njcn.web.utils.ReflectCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
/**
* @author caozehui
* @data 2024-12-2
*/
@Slf4j
@ControllerAdvice
public class LogAdvice implements ResponseBodyAdvice<Object> {
@Autowired
private ISysLogAuditService logService;
private final ThreadPoolExecutor executor = new ThreadPoolExecutor(
4, 8, 30, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100),
// 队列满时由主线程执行
new ThreadPoolExecutor.CallerRunsPolicy()
);
/**
* 无需审计记录的操作,比如根据客户端获取客户端信息
*/
private final static List<String> UN_LOG_INFO = Collections.singletonList("未知业务");
/**
* controller返回的响应体包含的状态码而非全局异常捕获器处抛出来的状态码
*/
private final static List<String> FILTER_CODE = Arrays.asList(CommonResponseEnum.SUCCESS.getCode(), CommonResponseEnum.FAIL.getCode(), CommonResponseEnum.NO_DATA.getCode());
/**
* 判断下结果,是不是成功
*
* @param returnType 返回类型包含大量信息controller、method、result等等
* @param converterType 消息转换器类型目前配置的是Jackson
*/
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
/**
* 拦截所有请求成功的操作进行日志入库处理
* 需要 用户标识、事件描述、事件结果、操作IP、事件类型、事件严重度、操作时间、操作类型
*
* @param body .
* @param returnType .
* @param selectedContentType .
* @param selectedConverterType .
* @param request .
* @param response .
* @return .
*/
@Override
public Object beforeBodyWrite(Object body, @Nonnull MethodParameter returnType, @Nonnull MediaType selectedContentType, @Nonnull Class selectedConverterType, @Nonnull ServerHttpRequest request, @Nonnull ServerHttpResponse response) {
if (body instanceof HttpResult) {
HttpResult<?> httpResult = (HttpResult<?>) body;
if (FILTER_CODE.contains(httpResult.getCode())) {
// 传递上下文
HttpServletRequest httpServletRequest = HttpServletUtil.getRequest();
String methodDescribe = ReflectCommonUtil.getMethodDescribeByMethod(returnType.getMethod());
if (!UN_LOG_INFO.contains(methodDescribe)) {
Future<?> future = executor.submit(() -> {
HttpServletUtil.setRequest(httpServletRequest);
logService.recodeAdviceLog(returnType, httpResult, methodDescribe);
});
try {
// 抛出 ExecutionException
future.get();
} catch (ExecutionException | InterruptedException e) {
log.error("保存审计日志异常,异常为:"+e.getMessage());
}
}
}
}
return body;
}
}

View File

@@ -6,11 +6,14 @@ import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.system.log.service.ISysLogAuditService;
import com.njcn.web.utils.HttpResultUtil;
import com.njcn.web.utils.HttpServletUtil;
import com.njcn.web.utils.ReflectCommonUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -18,6 +21,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.util.NestedServletException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
@@ -25,6 +29,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -40,7 +45,16 @@ import java.util.stream.Stream;
@RestControllerAdvice
public class GlobalBusinessExceptionHandler {
// private final ILogService logService;
@Resource
private final ISysLogAuditService sysLogAuditService;
private final ThreadPoolExecutor executor = new ThreadPoolExecutor(
4, 8, 30, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100),
// 队列满时由主线程执行
new ThreadPoolExecutor.CallerRunsPolicy()
);
/**
* 捕获业务功能异常,通常为业务数据抛出的异常
@@ -48,16 +62,9 @@ public class GlobalBusinessExceptionHandler {
* @param businessException 业务异常
*/
@ExceptionHandler(BusinessException.class)
public HttpResult<String> handleBusinessException(HttpServletRequest httpServletRequest, BusinessException businessException) {
public HttpResult<String> handleBusinessException(BusinessException businessException) {
String operate = ReflectCommonUtil.getMethodDescribeByException(businessException);
// logService.recodeBusinessExceptionLog(businessException, httpServletRequest, businessException.getMessage());
//判断方法上是否有自定义注解,做特殊处理
Method method = ReflectCommonUtil.getMethod(businessException);
// if (!Objects.isNull(method)){
// if(method.isAnnotationPresent(ReturnMsg.class)){
// return HttpResultUtil.assembleResult(businessException.getCode(), null, StrFormatter.format("{}",businessException.getMessage()));
// }
// }
recodeAdviceLog(businessException, businessException.getMessage());
return HttpResultUtil.assembleBusinessExceptionResult(businessException, null, operate);
}
@@ -68,9 +75,9 @@ public class GlobalBusinessExceptionHandler {
* @param nullPointerException 空指针异常
*/
@ExceptionHandler(NullPointerException.class)
public HttpResult<String> handleNullPointerException(HttpServletRequest httpServletRequest, NullPointerException nullPointerException) {
public HttpResult<String> handleNullPointerException(NullPointerException nullPointerException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.NULL_POINTER_EXCEPTION.getMessage(), nullPointerException);
// logService.recodeBusinessExceptionLog(nullPointerException, httpServletRequest, CommonResponseEnum.NULL_POINTER_EXCEPTION.getMessage());
recodeAdviceLog(nullPointerException, CommonResponseEnum.NULL_POINTER_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NULL_POINTER_EXCEPTION, null, ReflectCommonUtil.getMethodDescribeByException(nullPointerException));
}
@@ -80,9 +87,9 @@ public class GlobalBusinessExceptionHandler {
* @param arithmeticException 算数运算异常由于除数为0引起的异常
*/
@ExceptionHandler(ArithmeticException.class)
public HttpResult<String> handleArithmeticException(HttpServletRequest httpServletRequest, ArithmeticException arithmeticException) {
public HttpResult<String> handleArithmeticException(ArithmeticException arithmeticException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.ARITHMETIC_EXCEPTION.getMessage(), arithmeticException);
// logService.recodeBusinessExceptionLog(arithmeticException, httpServletRequest, CommonResponseEnum.ARITHMETIC_EXCEPTION.getMessage());
recodeAdviceLog(arithmeticException, CommonResponseEnum.ARITHMETIC_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.ARITHMETIC_EXCEPTION, null, ReflectCommonUtil.getMethodDescribeByException(arithmeticException));
}
@@ -92,9 +99,9 @@ public class GlobalBusinessExceptionHandler {
* @param classCastException 类型转换异常
*/
@ExceptionHandler(ClassCastException.class)
public HttpResult<String> handleClassCastException(HttpServletRequest httpServletRequest, ClassCastException classCastException) {
public HttpResult<String> handleClassCastException(ClassCastException classCastException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.CLASS_CAST_EXCEPTION.getMessage(), classCastException);
// logService.recodeBusinessExceptionLog(classCastException, httpServletRequest, CommonResponseEnum.CLASS_CAST_EXCEPTION.getMessage());
recodeAdviceLog(classCastException, CommonResponseEnum.CLASS_CAST_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.CLASS_CAST_EXCEPTION, null, ReflectCommonUtil.getMethodDescribeByException(classCastException));
}
@@ -105,9 +112,9 @@ public class GlobalBusinessExceptionHandler {
* @param indexOutOfBoundsException 索引下标越界异常
*/
@ExceptionHandler(IndexOutOfBoundsException.class)
public HttpResult<String> handleIndexOutOfBoundsException(HttpServletRequest httpServletRequest, IndexOutOfBoundsException indexOutOfBoundsException) {
public HttpResult<String> handleIndexOutOfBoundsException(IndexOutOfBoundsException indexOutOfBoundsException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.INDEX_OUT_OF_BOUNDS_EXCEPTION.getMessage(), indexOutOfBoundsException);
// logService.recodeBusinessExceptionLog(indexOutOfBoundsException, httpServletRequest, CommonResponseEnum.INDEX_OUT_OF_BOUNDS_EXCEPTION.getMessage());
recodeAdviceLog(indexOutOfBoundsException, CommonResponseEnum.INDEX_OUT_OF_BOUNDS_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.INDEX_OUT_OF_BOUNDS_EXCEPTION, null, ReflectCommonUtil.getMethodDescribeByException(indexOutOfBoundsException));
}
@@ -117,10 +124,10 @@ public class GlobalBusinessExceptionHandler {
* @param httpMediaTypeNotSupportedException 请求中参数的媒体方式不支持异常
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public HttpResult<String> httpMediaTypeNotSupportedExceptionHandler(HttpServletRequest httpServletRequest, HttpMediaTypeNotSupportedException httpMediaTypeNotSupportedException) {
public HttpResult<String> httpMediaTypeNotSupportedExceptionHandler(HttpMediaTypeNotSupportedException httpMediaTypeNotSupportedException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION.getMessage(), httpMediaTypeNotSupportedException);
// 然后提取错误提示信息进行返回
// logService.recodeBusinessExceptionLog(httpMediaTypeNotSupportedException, httpServletRequest, CommonResponseEnum.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION.getMessage());
recodeAdviceLog(httpMediaTypeNotSupportedException, CommonResponseEnum.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION, null, ReflectCommonUtil.getMethodDescribeByException(httpMediaTypeNotSupportedException));
}
@@ -131,13 +138,13 @@ public class GlobalBusinessExceptionHandler {
* @param methodArgumentNotValidException 参数校验异常
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public HttpResult<String> methodArgumentNotValidExceptionHandler(HttpServletRequest httpServletRequest, MethodArgumentNotValidException methodArgumentNotValidException) {
public HttpResult<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException methodArgumentNotValidException) {
// 从异常对象中拿到allErrors数据
String messages = methodArgumentNotValidException.getBindingResult().getAllErrors()
.stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(""));
// 然后提取错误提示信息进行返回
LogUtil.njcnDebug(log, "参数校验异常,异常为:{}", messages);
// logService.recodeBusinessExceptionLog(methodArgumentNotValidException, httpServletRequest, CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION.getMessage());
recodeAdviceLog(methodArgumentNotValidException, CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION, messages, ControllerUtil.getMethodArgumentNotValidException(methodArgumentNotValidException));
}
@@ -148,7 +155,7 @@ public class GlobalBusinessExceptionHandler {
* @param constraintViolationException 参数校验异常
*/
@ExceptionHandler(ConstraintViolationException.class)
public HttpResult<String> constraintViolationExceptionExceptionHandler(HttpServletRequest httpServletRequest, ConstraintViolationException constraintViolationException) {
public HttpResult<String> constraintViolationExceptionExceptionHandler(ConstraintViolationException constraintViolationException) {
String exceptionMessage = constraintViolationException.getMessage();
StringBuilder messages = new StringBuilder();
if (exceptionMessage.indexOf(StrUtil.COMMA) > 0) {
@@ -161,7 +168,7 @@ public class GlobalBusinessExceptionHandler {
}
// 然后提取错误提示信息进行返回
LogUtil.njcnDebug(log, "参数校验异常,异常为:{}", messages);
// logService.recodeBusinessExceptionLog(constraintViolationException, httpServletRequest, CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION.getMessage());
recodeAdviceLog(constraintViolationException, CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION.getMessage());
List<ConstraintViolation<?>> constraintViolationList = new ArrayList<>(constraintViolationException.getConstraintViolations());
ConstraintViolation<?> constraintViolation = constraintViolationList.get(0);
Class<?> rootBeanClass = constraintViolation.getRootBeanClass();
@@ -182,24 +189,12 @@ public class GlobalBusinessExceptionHandler {
* @param illegalArgumentException 参数校验异常
*/
@ExceptionHandler(IllegalArgumentException.class)
public HttpResult<String> handleIndexOutOfBoundsException(HttpServletRequest httpServletRequest, IllegalArgumentException illegalArgumentException) {
public HttpResult<String> handleIndexOutOfBoundsException(IllegalArgumentException illegalArgumentException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION.getMessage(), illegalArgumentException);
// logService.recodeBusinessExceptionLog(illegalArgumentException, httpServletRequest, CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION.getMessage());
recodeAdviceLog(illegalArgumentException, CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION, illegalArgumentException.getMessage(), ReflectCommonUtil.getMethodDescribeByException(illegalArgumentException));
}
// /**
// * 表格校验异常
// *
// * @param excelAnalysisException 表格校验异常
// */
// @ExceptionHandler(ExcelAnalysisException.class)
// public HttpResult<String> handleExcelAnalysisException(HttpServletRequest httpServletRequest, ExcelAnalysisException excelAnalysisException) {
// LogUtil.logExceptionStackInfo(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION.getMessage(), excelAnalysisException);
// // logService.recodeBusinessExceptionLog(excelAnalysisException, httpServletRequest,CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION.getMessage());
// return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION, excelAnalysisException.getCause().getMessage(), ReflectCommonUtil.getMethodDescribeByException(excelAnalysisException));
// }
/**
* 未声明异常捕捉
@@ -207,7 +202,7 @@ public class GlobalBusinessExceptionHandler {
* @param exception 未声明异常
*/
@ExceptionHandler(Exception.class)
public HttpResult<String> handleException(HttpServletRequest httpServletRequest, Exception exception) {
public HttpResult<String> handleException(Exception exception) {
//针对fallbackFactory降级异常特殊处理
Exception tempException = exception;
String exceptionCause = CommonResponseEnum.UN_DECLARE.getMessage();
@@ -224,7 +219,7 @@ public class GlobalBusinessExceptionHandler {
}
}
LogUtil.logExceptionStackInfo(exceptionCause, tempException);
// logService.recodeBusinessExceptionLog(tempException, httpServletRequest, exceptionCause);
recodeAdviceLog(exception, exceptionCause);
//判断方法上是否有自定义注解,做特殊处理
// Method method = ReflectCommonUtil.getMethod(exception);
// if (!Objects.isNull(method)){
@@ -236,18 +231,30 @@ public class GlobalBusinessExceptionHandler {
}
/**
* json解析异常
*
* @param jsonException json参数
*/
@ExceptionHandler(JSONException.class)
public HttpResult<String> handleIndexOutOfBoundsException(HttpServletRequest httpServletRequest, JSONException jsonException) {
public HttpResult<String> handleIndexOutOfBoundsException(JSONException jsonException) {
LogUtil.logExceptionStackInfo(CommonResponseEnum.JSON_CONVERT_EXCEPTION.getMessage(), jsonException);
// logService.recodeBusinessExceptionLog(jsonException, httpServletRequest, CommonResponseEnum.JSON_CONVERT_EXCEPTION.getMessage());
recodeAdviceLog(jsonException, CommonResponseEnum.JSON_CONVERT_EXCEPTION.getMessage());
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.JSON_CONVERT_EXCEPTION, jsonException.getMessage(), ReflectCommonUtil.getMethodDescribeByException(jsonException));
}
private void recodeAdviceLog(Exception businessException, String methodDescribe) {
HttpServletRequest httpServletRequest = HttpServletUtil.getRequest();
Future<?> future = executor.submit(() -> {
HttpServletUtil.setRequest(httpServletRequest);
sysLogAuditService.recodeBusinessExceptionLog(businessException, methodDescribe);
});
try {
// 抛出 ExecutionException
future.get();
} catch (ExecutionException | InterruptedException e) {
log.error("保存审计日志异常,异常为:" + e.getMessage());
}
}
}

View File

@@ -0,0 +1,37 @@
package com.njcn;
import com.njcn.gather.EntranceApplication;
import com.njcn.gather.report.pojo.DevReportParam;
import com.njcn.gather.report.service.IPqReportService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* @author hongawen
* @version 1.0.0
* @date 2021年12月10日 15:05
*/
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = EntranceApplication.class)
public class BaseJunitTest {
@Autowired
private IPqReportService pqReportService;
@Test
public void test() {
DevReportParam devReportParam = new DevReportParam();
devReportParam.setPlanId("ad3df9e4a90b4c3c8ce7d21a84ce6f59");
devReportParam.setPlanCode("31");
devReportParam.setScriptId("810e4050e1d445e3542c998a077a263a");
devReportParam.setDevId("a46349a3b3cf4789a6b82690a6076afd");
pqReportService.generateReport(devReportParam);
}
}