Commit 8da1baad authored by 唐峰's avatar 唐峰

日志信息增加traceId,异常响应结果输出

parent b1101470
......@@ -3,6 +3,8 @@ package cn.quantgroup.xyqb.controller;
import cn.quantgroup.xyqb.exception.*;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.util.IpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.BadSqlGrammarException;
......@@ -41,7 +43,9 @@ public class ExceptionHandlingController implements IBaseController {
@ExceptionHandler(PasswordErrorLimitException.class)
public JsonResult passwordErrorLimitException(PasswordErrorLimitException vce) {
log.info("throw PasswordErrorLimitException,msg={},businessCode={}, clientIp={}", vce.getMessage(), 2L, IpUtil.getRemoteIP(getRequest()));
return JsonResult.buildErrorStateResult(vce.getMessage(), null, 2L);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(vce.getMessage(), null, 2L);
log.info("PasswordErrorLimitException异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),vce);
return objectJsonResult;
}
/**
......@@ -53,7 +57,9 @@ public class ExceptionHandlingController implements IBaseController {
@ExceptionHandler(VerificationCodeErrorException.class)
public JsonResult verificationCodeErrorException(VerificationCodeErrorException vce) {
log.info("throw VerificationCodeErrorException,msg={},businessCode={}, clientIp={}", vce.getMessage(), 1L, IpUtil.getRemoteIP(getRequest()));
return JsonResult.buildErrorStateResult(vce.getMessage(), null, 1L);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(vce.getMessage(), null, 1L);
log.info("VerificationCodeErrorException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),vce);
return objectJsonResult;
}
/**
......@@ -66,7 +72,9 @@ public class ExceptionHandlingController implements IBaseController {
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public JsonResult userNotExistException(UserNotExistException unee) {
log.info("throw UserNotExistException,msg={},businessCode={},code={}", unee.getMessage(), 1L, (long) HttpStatus.UNAUTHORIZED.value());
return new JsonResult(unee.getMessage(), (long) HttpStatus.UNAUTHORIZED.value(), null);
JsonResult<Object> objectJsonResult = new JsonResult(unee.getMessage(), (long) HttpStatus.UNAUTHORIZED.value(), null);
log.info("userNotExistException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),unee);
return objectJsonResult;
}
/**
......@@ -78,7 +86,9 @@ public class ExceptionHandlingController implements IBaseController {
@ExceptionHandler(UserQueryLogException.class)
public JsonResult userQueryLogException(UserQueryLogException uqle) {
log.info("throw UserQueryLogException,msg={},businessCode={},code={}", uqle.getMessage(), 1L, 0L);
return JsonResult.buildErrorStateResult(uqle.getMessage(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(uqle.getMessage(), null);
log.info("userQueryLogException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),uqle);
return objectJsonResult;
}
/**
......@@ -91,7 +101,9 @@ public class ExceptionHandlingController implements IBaseController {
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public JsonResult wechatRelateUserException(WechatRelateUserException wrue) {
log.info("throw WechatRelateUserException,msg={},businessCode={},code={}", wrue.getMessage(), 1L, (long) HttpStatus.UNAUTHORIZED.value());
return new JsonResult(wrue.getMessage(), (long) HttpStatus.UNAUTHORIZED.value(), null);
JsonResult<Object> objectJsonResult = new JsonResult(wrue.getMessage(), (long) HttpStatus.UNAUTHORIZED.value(), null);
log.info("wechatRelateUserException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),wrue);
return objectJsonResult;
}
/**
......@@ -106,7 +118,9 @@ public class ExceptionHandlingController implements IBaseController {
String uri = request.getRequestURI();
String registerFrom = request.getParameter("registerFrom");
log.error("[exception][global_exception]接口异常 URI:{}, registerFrom:{},error={}", uri, registerFrom, e);
return EXCEPTION_RESULT;
JsonResult<Object> objectJsonResult = EXCEPTION_RESULT;
log.info("exceptionOccurs 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler(ValidationException.class)
......@@ -118,7 +132,9 @@ public class ExceptionHandlingController implements IBaseController {
return null;
}
String message = constraintViolations.iterator().next().getMessage();
return JsonResult.buildErrorStateResult(message, null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(message, null);
log.info("validException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),exception);
return objectJsonResult;
}
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
......@@ -147,7 +163,9 @@ public class ExceptionHandlingController implements IBaseController {
}
}
}
return JsonResult.buildErrorStateResult(sb.toString(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(sb.toString(), null);
log.info("handleBindException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
return null;
}
......@@ -156,13 +174,17 @@ public class ExceptionHandlingController implements IBaseController {
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public JsonResult handleDataException(DataException e) {
return JsonResult.buildErrorStateResult(e.getMessage(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(e.getMessage(), null);
log.info("handleDataException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler({AppletException.class})
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public JsonResult handleAppletException(AppletException e) {
return JsonResult.buildErrorResultWhithCode(e.getMessage(), e.getCode());
JsonResult<Object> objectJsonResult = JsonResult.buildErrorResultWhithCode(e.getMessage(), e.getCode());
log.info("handleAppletException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler(BadSqlGrammarException.class)
......@@ -170,21 +192,27 @@ public class ExceptionHandlingController implements IBaseController {
@ResponseStatus(HttpStatus.OK)
public JsonResult handleBadSqlGrammarException(BadSqlGrammarException e) {
log.error("sql语法解析异常 error sql = 【{}】", e.getSql(), e);
return JsonResult.buildErrorStateResult("参数错误。", null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult("参数错误。", null);
log.info("handleBadSqlGrammarException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler(ResubmissionException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public JsonResult resubmissionException(ResubmissionException re) {
return JsonResult.buildErrorStateResult(re.getMessage(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(re.getMessage(), null);
log.info("resubmissionException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),re);
return objectJsonResult;
}
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public JsonResult handelMissingServletRequestParameterException(MissingServletRequestParameterException re) {
return JsonResult.buildErrorStateResult(re.getMessage(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(re.getMessage(), null);
log.info("handelMissingServletRequestParameterException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),re);
return objectJsonResult;
}
/**
......@@ -194,20 +222,28 @@ public class ExceptionHandlingController implements IBaseController {
*/
@ExceptionHandler(UserRegisterLoginException.class)
public JsonResult<?> handleUserDeregisterException(UserRegisterLoginException e) {
return JsonResult.buildErrorStateResult(e.getMessage(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(e.getMessage(), null);
log.info("handleUserDeregisterException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler(AccessForbiddenException.class)
public JsonResult<?> handleAccessForbiddenException(AccessForbiddenException e) {
return JsonResult.buildErrorStateResult(e.getMessage(), null);
JsonResult<Object> objectJsonResult = JsonResult.buildErrorStateResult(e.getMessage(), null);
log.info("handleAccessForbiddenException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler(BizException.class)
public JsonResult<?> handleBizException(BizException e) {
return JsonResult.buildErrorResultWhithCode(e.getMsg(),e.getBusinessCode());
JsonResult<Object> objectJsonResult = JsonResult.buildErrorResultWhithCode(e.getMsg(),e.getBusinessCode());
log.info("handleBizException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
@ExceptionHandler(SilentBizException.class)
public JsonResult<?> handleSilentBizException(SilentBizException e) {
return JsonResult.buildErrorResultWhithCode(e.getMsg(),e.getBusinessCode());
JsonResult<Object> objectJsonResult = JsonResult.buildErrorResultWhithCode(e.getMsg(),e.getBusinessCode());
log.info("handleSilentBizException 异常响应结果:{}", JSONObject.toJSONString(objectJsonResult),e);
return objectJsonResult;
}
}
package cn.quantgroup.xyqb.model;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.util.LogTraceIdUtil;
import lombok.Data;
import java.io.Serializable;
......@@ -29,13 +30,17 @@ public class JsonResult<T> implements Serializable {
// 业务错误码
private String businessCode = Constants.SUCCESS_CODE;
private String traceId;
private T data = null;
public JsonResult() {
this.traceId = LogTraceIdUtil.getTraceId();
}
public JsonResult(T data) {
this.data = data;
this.traceId = LogTraceIdUtil.getTraceId();
}
/**
......@@ -47,6 +52,7 @@ public class JsonResult<T> implements Serializable {
this.msg = msg;
this.code = String.format(Constants.ZERO_FILL_TEMPLATE, code);
this.data = data;
this.traceId = LogTraceIdUtil.getTraceId();
}
public JsonResult(String msg, Long code, T data, Long businessCode) {
......@@ -54,6 +60,7 @@ public class JsonResult<T> implements Serializable {
this.code = String.format(Constants.ZERO_FILL_TEMPLATE, code);
this.data = data;
this.businessCode = String.format(Constants.ZERO_FILL_TEMPLATE, businessCode);
this.traceId = LogTraceIdUtil.getTraceId();
}
/**
......
package cn.quantgroup.xyqb.util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
/**
* @Describe:
* @Created by tangfeng 2021/12/11 12:18 下午
*/
public class LogTraceIdUtil {
public static String getTraceId(){
String traceId = MDC.get("X-B3-TraceId");
String spanId = MDC.get("X-B3-SpanId");
if (StringUtils.isNotEmpty(traceId) && StringUtils.isNotEmpty(spanId)) {
StringBuffer sb = new StringBuffer()
.append("[").append(traceId).append("-").append(spanId).append("]");
return sb.toString();
}
/*String PtxId = MDC.get("PtxId");
if(StringUtils.isNotEmpty(PtxId)){
StringBuffer sb = new StringBuffer()
.append("[").append(PtxId).append("]");
return sb.toString();
}*/
return "";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment