Browse Source

update api log

deason 3 năm trước cách đây
mục cha
commit
cfc0d50a8a

+ 298 - 300
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/cloud/CloudClientSupport.java

@@ -1,39 +1,30 @@
 package cn.com.qmth.examcloud.web.cloud;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import cn.com.qmth.examcloud.web.config.SystemProperties;
-import org.apache.commons.collections.CollectionUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.core.io.FileSystemResource;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-import org.springframework.web.client.RestTemplate;
-
 import cn.com.qmth.examcloud.api.commons.exchange.BaseRequest;
 import cn.com.qmth.examcloud.api.commons.exchange.FormFilePart;
 import cn.com.qmth.examcloud.api.commons.exchange.FormRequest;
 import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
 import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.commons.util.ByteUtil;
-import cn.com.qmth.examcloud.commons.util.JsonUtil;
-import cn.com.qmth.examcloud.commons.util.SHA256;
-import cn.com.qmth.examcloud.commons.util.StringUtil;
-import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
+import cn.com.qmth.examcloud.commons.util.*;
 import cn.com.qmth.examcloud.web.config.LogProperties;
+import cn.com.qmth.examcloud.web.config.SystemProperties;
 import cn.com.qmth.examcloud.web.exception.ApiFlowLimitedException;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
 import cn.com.qmth.examcloud.web.support.StatusResponse;
+import org.apache.commons.collections.CollectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.*;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 /**
  * 云服务客户端基类
@@ -44,279 +35,286 @@ import cn.com.qmth.examcloud.web.support.StatusResponse;
  */
 public abstract class CloudClientSupport {
 
-	protected static final Logger LOGGER = LoggerFactory.getLogger(CloudClientSupport.class);
-
-	private RestTemplate restTemplate;
-
-	private LogProperties logProperties;
-
-	private static String[] excludeFields = new String[]{"password", ".*Password"};
-
-	private LogProperties getLogProperties() {
-		if (null == logProperties) {
-			logProperties = SpringContextHolder.getBean(LogProperties.class);
-		}
-		return logProperties;
-	}
-
-	/**
-	 * 获取请求映射前缀
-	 *
-	 * @author WANGWEI
-	 * @return
-	 */
-	protected abstract String getRequestMappingPrefix();
-
-	/**
-	 * 获取 RestTemplate
-	 *
-	 * @author WANGWEI
-	 * @return
-	 */
-	private RestTemplate getRestTemplate() {
-		if (null == restTemplate) {
-			restTemplate = SpringContextHolder.getBean(RestTemplate.class);
-		}
-		return restTemplate;
-	}
-
-	/**
-	 * 构建url
-	 *
-	 * @author WANGWEI
-	 * @param appName
-	 * @param requestMapping
-	 * @return
-	 */
-	private String buildUrl(String appName, String requestMapping) {
-		StringBuilder sb = new StringBuilder();
-		sb.append("http://");
-		String redirection = CloudServiceRedirector.getRedirection(appName);
-		if (null != redirection) {
-			sb.append(redirection);
-		} else {
-			sb.append(appName);
-		}
-		String rmp = getRequestMappingPrefix();
-		if (rmp.startsWith("/")) {
-			sb.append(rmp);
-		} else {
-			sb.append("/").append(rmp);
-		}
-		if ('/' == sb.charAt(sb.length() - 1) && requestMapping.endsWith("/")) {
-			sb.deleteCharAt(sb.length() - 1).append(requestMapping);
-		} else if ('/' != sb.charAt(sb.length() - 1) && !requestMapping.endsWith("/")) {
-			sb.append("/").append(requestMapping);
-		} else {
-			sb.append(requestMapping);
-		}
-
-		return sb.toString();
-	}
-
-	/**
-	 * 获取响应体
-	 * 
-	 * @author WANGWEI
-	 * @param respEntity
-	 * @return
-	 */
-	private <T> T getRespBody(ResponseEntity<String> respEntity, Class<T> responseType) {
-
-		String body = respEntity.getBody();
-		if (HttpStatus.OK == respEntity.getStatusCode()) {
-			if (null == responseType) {
-				return null;
-			}
-			T t = JsonUtil.fromJson(body, responseType);
-			return t;
-		} else if (HttpStatus.INTERNAL_SERVER_ERROR == respEntity.getStatusCode()) {
-			StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
-			throw new StatusException(sr.getCode(), sr.getDesc());
-		} else if (HttpStatus.SERVICE_UNAVAILABLE == respEntity.getStatusCode()) {
-			StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
-			throw new ApiFlowLimitedException(sr.getCode(), sr.getDesc());
-		} else {
-			throw new ExamCloudRuntimeException(
-					"unexpected http status code [" + respEntity.getStatusCode() + "]");
-		}
-	}
-
-	/**
-	 * exchange
-	 *
-	 * @author WANGWEI
-	 * @param url
-	 * @param method
-	 * @param body
-	 * @param responseType
-	 * @return
-	 */
-	protected <T> T exchange(String url, HttpMethod method, Object body, Class<T> responseType) {
-		return exchange(url, method, null, body, responseType);
-	}
-
-	/**
-	 * exchange
-	 *
-	 * @author WANGWEI
-	 * @param url
-	 * @param method
-	 * @param httpHeaders
-	 * @param body
-	 * @param responseType
-	 * @return
-	 */
-	protected <T> T exchange(String url, HttpMethod method, HttpHeaders httpHeaders, Object body,
-			Class<T> responseType) {
-
-		long startTime = System.currentTimeMillis();
-
-		if (null == httpHeaders) {
-			httpHeaders = new HttpHeaders();
-		}
-
-		httpHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
-		httpHeaders.add("timestamp", String.valueOf(startTime));
-		httpHeaders.add("App-Id", String.valueOf(SystemProperties.APP_ID));
-		httpHeaders.add("App-Code", String.valueOf(SystemProperties.APP_CODE));
-
-		String joinStr = StringUtil.join(
-				SystemProperties.APP_ID,
-				SystemProperties.APP_CODE,
-				startTime,
-				SystemProperties.APP_SECRET_KEY);
-
-		byte[] bytes = SHA256.encode(joinStr);
-		String accessToken = ByteUtil.toHexAscii(bytes);
-		httpHeaders.add("Access-Token", accessToken);
-
-		if (LOGGER.isInfoEnabled()) {
-			LOGGER.info("[CALL-IN]. url=" + url);
-			if (null == body) {
-				LOGGER.info("[CALL-REQ]. request= void");
-			} else if (body instanceof JsonSerializable) {
-				LOGGER.info("[CALL-REQ]. request=" + JsonUtil.toJson(body, excludeFields));
-			}
-		}
-
-		HttpEntity<Object> requestEntity = null;
-		if (null == body) {
-			requestEntity = new HttpEntity<Object>(httpHeaders);
-		} else {
-			requestEntity = new HttpEntity<Object>(body, httpHeaders);
-		}
-
-		T respBody = null;
-		try {
-			ResponseEntity<String> respEntity = getRestTemplate().exchange(url, method,
-					requestEntity, String.class);
-			respBody = getRespBody(respEntity, responseType);
-
-			if (LOGGER.isDebugEnabled() && getLogProperties().isNormalResponseLogEnable()) {
-				String respEntityBody = respEntity.getBody();
-				int responseJsonMaxSize = getLogProperties().getResponseLogJsonMaxSize();
-				if (null == respEntityBody) {
-					LOGGER.debug("[CALL-RESP]. response= void");
-				} else if (respEntityBody.length() > responseJsonMaxSize) {
-					LOGGER.debug("[CALL-RESP]. response= too large");
-				} else {
-					LOGGER.debug("[CALL-RESP]. response=" + respEntityBody);
-				}
-			}
-			if (LOGGER.isInfoEnabled()) {
-				LOGGER.info(StringUtil.join("[CALL-OK]. url=" + url,
-						" ; cost " + (System.currentTimeMillis() - startTime), " ms."));
-			}
-		} catch (ApiFlowLimitedException e) {
-			LOGGER.error(StringUtil.join("[CALL-FAIL]. url=" + url,
-					" ; cost " + (System.currentTimeMillis() - startTime), " ms."));
-			LOGGER.error("[CALL-RESP]. response=" + e.toJson());
-			throw e;
-		} catch (StatusException e) {
-			LOGGER.error(StringUtil.join("[CALL-FAIL]. url=" + url,
-					" ; cost " + (System.currentTimeMillis() - startTime), " ms."));
-			LOGGER.error("[CALL-RESP]. response=" + e.toJson());
-			throw e;
-		} catch (Exception e) {
-			LOGGER.error(StringUtil.join("[CALL-FATAL]. url=" + url, " ;", e.getMessage()));
-			throw e;
-		}
-
-		return respBody;
-	}
-
-	/**
-	 * post请求
-	 *
-	 * @author WANGWEI
-	 * @param requestMapping
-	 * @param body
-	 * @param responseType
-	 * @return
-	 */
-	protected <T> T post(String appName, String requestMapping, BaseRequest body,
-			Class<T> responseType) {
-		String url = buildUrl(appName, requestMapping);
-		return exchange(url, HttpMethod.POST, body, responseType);
-	}
-
-	/**
-	 * post请求
-	 * 
-	 * @param requestMapping
-	 * @param body
-	 */
-	protected void post(String appName, String requestMapping, BaseRequest body) {
-		String url = buildUrl(appName, requestMapping);
-		exchange(url, HttpMethod.POST, body, null);
-	}
-
-	/**
-	 * post请求
-	 * 
-	 * @param requestMapping
-	 */
-	protected void post(String appName, String requestMapping) {
-		String url = buildUrl(appName, requestMapping);
-		exchange(url, HttpMethod.POST, null, null);
-	}
-
-	/**
-	 * 文件表单提交
-	 *
-	 * @author WANGWEI
-	 * @param requestMapping
-	 * @param req
-	 * @param responseType
-	 * @return
-	 * @throws Exception
-	 */
-	public <T> T postForm(String appName, String requestMapping, FormRequest req,
-			Class<T> responseType) {
-		String url = buildUrl(appName, requestMapping);
-
-		MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
-		HttpHeaders httpHeaders = new HttpHeaders();
-		httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
-
-		List<FormFilePart> formFilePartList = req.getFormFilePartList();
-		if (CollectionUtils.isNotEmpty(formFilePartList)) {
-			for (FormFilePart part : formFilePartList) {
-				FileSystemResource resource = new CustomFileSystemResource(part.getFile(),
-						part.getFilename());
-				params.add(part.getParamName(), resource);
-			}
-		}
-
-		String json = JsonUtil.toJson(req);
-		Map<String, String> otherParams = JsonUtil.json2Map(json);
-
-		for (Entry<String, String> entry : otherParams.entrySet()) {
-			params.add(entry.getKey(), entry.getValue());
-		}
-
-		return exchange(url, HttpMethod.POST, httpHeaders, params, responseType);
-	}
-
-}
+    protected static final Logger LOGGER = LoggerFactory.getLogger(CloudClientSupport.class);
+
+    private RestTemplate restTemplate;
+
+    private LogProperties logProperties;
+
+    private static String[] excludeFields = new String[]{"password", ".*Password"};
+
+    private LogProperties getLogProperties() {
+        if (null == logProperties) {
+            logProperties = SpringContextHolder.getBean(LogProperties.class);
+        }
+        return logProperties;
+    }
+
+    /**
+     * 获取请求映射前缀
+     *
+     * @return
+     * @author WANGWEI
+     */
+    protected abstract String getRequestMappingPrefix();
+
+    /**
+     * 获取 RestTemplate
+     *
+     * @return
+     * @author WANGWEI
+     */
+    private RestTemplate getRestTemplate() {
+        if (null == restTemplate) {
+            restTemplate = SpringContextHolder.getBean(RestTemplate.class);
+        }
+        return restTemplate;
+    }
+
+    /**
+     * 构建url
+     *
+     * @param appName
+     * @param requestMapping
+     * @return
+     * @author WANGWEI
+     */
+    private String buildUrl(String appName, String requestMapping) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("http://");
+        String redirection = CloudServiceRedirector.getRedirection(appName);
+        if (null != redirection) {
+            sb.append(redirection);
+        } else {
+            sb.append(appName);
+        }
+        String rmp = getRequestMappingPrefix();
+        if (rmp.startsWith("/")) {
+            sb.append(rmp);
+        } else {
+            sb.append("/").append(rmp);
+        }
+        if ('/' == sb.charAt(sb.length() - 1) && requestMapping.endsWith("/")) {
+            sb.deleteCharAt(sb.length() - 1).append(requestMapping);
+        } else if ('/' != sb.charAt(sb.length() - 1) && !requestMapping.endsWith("/")) {
+            sb.append("/").append(requestMapping);
+        } else {
+            sb.append(requestMapping);
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * 获取响应体
+     *
+     * @param respEntity
+     * @return
+     * @author WANGWEI
+     */
+    private <T> T getRespBody(ResponseEntity<String> respEntity, Class<T> responseType) {
+
+        String body = respEntity.getBody();
+        if (HttpStatus.OK == respEntity.getStatusCode()) {
+            if (null == responseType) {
+                return null;
+            }
+            T t = JsonUtil.fromJson(body, responseType);
+            return t;
+        } else if (HttpStatus.INTERNAL_SERVER_ERROR == respEntity.getStatusCode()) {
+            StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
+            throw new StatusException(sr.getCode(), sr.getDesc());
+        } else if (HttpStatus.SERVICE_UNAVAILABLE == respEntity.getStatusCode()) {
+            StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
+            throw new ApiFlowLimitedException(sr.getCode(), sr.getDesc());
+        } else {
+            throw new ExamCloudRuntimeException("unexpected http status code [" + respEntity.getStatusCode() + "]");
+        }
+    }
+
+    /**
+     * exchange
+     *
+     * @param url
+     * @param method
+     * @param body
+     * @param responseType
+     * @return
+     * @author WANGWEI
+     */
+    protected <T> T exchange(String url, HttpMethod method, Object body, Class<T> responseType) {
+        return exchange(url, method, null, body, responseType);
+    }
+
+    /**
+     * exchange
+     *
+     * @param url
+     * @param method
+     * @param httpHeaders
+     * @param body
+     * @param responseType
+     * @return
+     * @author WANGWEI
+     */
+    protected <T> T exchange(String url, HttpMethod method, HttpHeaders httpHeaders, Object body, Class<T> responseType) {
+        long startTime = System.currentTimeMillis();
+
+        if (null == httpHeaders) {
+            httpHeaders = new HttpHeaders();
+        }
+
+        httpHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
+        httpHeaders.add("timestamp", String.valueOf(startTime));
+        httpHeaders.add("App-Id", String.valueOf(SystemProperties.APP_ID));
+        httpHeaders.add("App-Code", String.valueOf(SystemProperties.APP_CODE));
+
+        String joinStr = StringUtil.join(
+                SystemProperties.APP_ID,
+                SystemProperties.APP_CODE,
+                startTime,
+                SystemProperties.APP_SECRET_KEY);
+
+        byte[] bytes = SHA256.encode(joinStr);
+        String accessToken = ByteUtil.toHexAscii(bytes);
+        httpHeaders.add("Access-Token", accessToken);
+
+        int responseJsonMaxSize = getLogProperties().getResponseLogJsonMaxSize();
+        if (LOGGER.isInfoEnabled() && getLogProperties().isNormalResponseLogEnable()) {
+            if (null == body) {
+                LOGGER.info("[RPC-request] - {}", url);
+            } else if (body instanceof JsonSerializable) {
+                String json = JsonUtil.toJson(body, excludeFields);
+                if (json == null) {
+                    LOGGER.info("[RPC-request] - {}", url);
+                } else {
+                    LOGGER.info("[RPC-request] - {} params: {}", url, this.cutStr(json, responseJsonMaxSize));
+                }
+            }
+        }
+
+        HttpEntity<Object> requestEntity;
+        if (null == body) {
+            requestEntity = new HttpEntity<>(httpHeaders);
+        } else {
+            requestEntity = new HttpEntity<>(body, httpHeaders);
+        }
+
+        T respBody = null;
+        try {
+            ResponseEntity<String> respEntity = getRestTemplate().exchange(url, method, requestEntity, String.class);
+            respBody = getRespBody(respEntity, responseType);
+
+            if (LOGGER.isDebugEnabled() && getLogProperties().isNormalResponseLogEnable()) {
+                String respEntityBody = respEntity.getBody();
+                if (null == respEntityBody) {
+                    LOGGER.debug("[RPC-response] - void");
+                } else {
+                    LOGGER.debug("[RPC-response] - {}", this.cutStr(respEntityBody, responseJsonMaxSize));
+                }
+            }
+
+            if (LOGGER.isInfoEnabled()) {
+                LOGGER.info("[RPC-ok] - {}, cost {} ms", url, System.currentTimeMillis() - startTime);
+            }
+        } catch (ApiFlowLimitedException e) {
+            LOGGER.error("[RPC-fail] - {}, cost {} ms, cause1 = {}", url, System.currentTimeMillis() - startTime, e.toJson());
+            throw e;
+        } catch (StatusException e) {
+            LOGGER.error("[RPC-fail] - {}, cost {} ms, cause2 = {}", url, System.currentTimeMillis() - startTime, e.toJson());
+            throw e;
+        } catch (Exception e) {
+            LOGGER.error("[RPC-fail] - {}, cost {} ms, cause3 = {}", url, System.currentTimeMillis() - startTime, e.getMessage());
+            throw e;
+        }
+
+        return respBody;
+    }
+
+    private String cutStr(String str, int maxLength) {
+        if (str == null) {
+            return "";
+        }
+
+        if (str.length() > maxLength) {
+            // content too large...
+            return str.substring(0, maxLength) + "......";
+        }
+
+        return str;
+    }
+
+    /**
+     * post请求
+     *
+     * @param requestMapping
+     * @param body
+     * @param responseType
+     * @return
+     * @author WANGWEI
+     */
+    protected <T> T post(String appName, String requestMapping, BaseRequest body,
+                         Class<T> responseType) {
+        String url = buildUrl(appName, requestMapping);
+        return exchange(url, HttpMethod.POST, body, responseType);
+    }
+
+    /**
+     * post请求
+     *
+     * @param requestMapping
+     * @param body
+     */
+    protected void post(String appName, String requestMapping, BaseRequest body) {
+        String url = buildUrl(appName, requestMapping);
+        exchange(url, HttpMethod.POST, body, null);
+    }
+
+    /**
+     * post请求
+     *
+     * @param requestMapping
+     */
+    protected void post(String appName, String requestMapping) {
+        String url = buildUrl(appName, requestMapping);
+        exchange(url, HttpMethod.POST, null, null);
+    }
+
+    /**
+     * 文件表单提交
+     *
+     * @param requestMapping
+     * @param req
+     * @param responseType
+     * @return
+     * @throws Exception
+     * @author WANGWEI
+     */
+    public <T> T postForm(String appName, String requestMapping, FormRequest req,
+                          Class<T> responseType) {
+        String url = buildUrl(appName, requestMapping);
+
+        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
+        HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
+
+        List<FormFilePart> formFilePartList = req.getFormFilePartList();
+        if (CollectionUtils.isNotEmpty(formFilePartList)) {
+            for (FormFilePart part : formFilePartList) {
+                FileSystemResource resource = new CustomFileSystemResource(part.getFile(),
+                        part.getFilename());
+                params.add(part.getParamName(), resource);
+            }
+        }
+
+        String json = JsonUtil.toJson(req);
+        Map<String, String> otherParams = JsonUtil.json2Map(json);
+
+        for (Entry<String, String> entry : otherParams.entrySet()) {
+            params.add(entry.getKey(), entry.getValue());
+        }
+
+        return exchange(url, HttpMethod.POST, httpHeaders, params, responseType);
+    }
+
+}

+ 38 - 35
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/support/ControllerAspect.java

@@ -101,14 +101,14 @@ public class ControllerAspect {
 
             // api method params
             if (ArrayUtils.isNotEmpty(args)) {
-                params.append(" methodParams: ");
+                params.append("params: ");
                 for (int i = 0; i < args.length; i++) {
                     Object curArg = args[i];
                     String content = this.parseContent(curArg);
                     params.append(content);
 
                     if (i < args.length - 1) {
-                        params.append(" | ");
+                        params.append(";");
                     }
                 }
             }
@@ -132,7 +132,9 @@ public class ControllerAspect {
                 }
             }
 
-            LOG.info(String.format("[request][%s] - %s %s", method, path, params.toString()));
+            if (LOG.isInfoEnabled() && logProperties.isNormalResponseLogEnable()) {
+                LOG.info("[HTTP-{}] - {}, {}", method, path, params);
+            }
         }
 
         Object ret;
@@ -144,20 +146,19 @@ public class ControllerAspect {
             // 执行
             ret = joinPoint.proceed();
 
-            request.setAttribute(HttpServletRequestAttribute.$_EXCEPTION_HAPPENED.name(), new Boolean(false));
+            request.setAttribute(HttpServletRequestAttribute.$_EXCEPTION_HAPPENED.name(), Boolean.FALSE);
         } catch (Throwable e) {
-            request.setAttribute(HttpServletRequestAttribute.$_EXCEPTION_HAPPENED.name(), new Boolean(true));
+            request.setAttribute(HttpServletRequestAttribute.$_EXCEPTION_HAPPENED.name(), Boolean.TRUE);
 
             if (null != httpMethodProcessor) {
                 try {
                     httpMethodProcessor.onException(request, args, e);
                 } catch (Exception ex) {
-                    LOG.error("processor1 - " + ex.getMessage(), ex);
+                    LOG.error("HTTP onException: {}", ex.getMessage(), ex);
                 }
             }
 
-            LOG.error(String.format("[execute fail][%s] - %s, cost %sms, err is %s"
-                    , method, path, System.currentTimeMillis() - startTime, e.getMessage()));
+            LOG.error("[HTTP-fail] - {}, cost {} ms, cause = {}", path, System.currentTimeMillis() - startTime, e.getMessage());
             throw new RuntimeException(e);
         }
 
@@ -165,7 +166,7 @@ public class ControllerAspect {
             try {
                 httpMethodProcessor.onSuccess(request, args, ret);
             } catch (Exception ex) {
-                LOG.error("processor2 - " + ex.getMessage(), ex);
+                LOG.error("HTTP onSuccess err: {}", ex.getMessage(), ex);
             }
         }
 
@@ -176,17 +177,16 @@ public class ControllerAspect {
 
         if (LOG.isDebugEnabled() && logProperties.isNormalResponseLogEnable()) {
             if (ret == null) {
-                LOG.debug("status = ok, responseMsg = void");
+                LOG.debug("[HTTP-{}] response status = 200, void", method);
             } else if (ret instanceof ResponseEntity) {
                 ResponseEntity<?> re = (ResponseEntity<?>) ret;
                 Object body = re.getBody();
 
                 String content = this.parseContent(body);
-                LOG.debug(String.format("status = %s, responseMsg = %s",
-                        re.getStatusCodeValue(), content));
+                LOG.debug("[HTTP-{}] response status = {}, {}", method, re.getStatusCodeValue(), content);
             } else {
                 String content = this.parseContent(ret);
-                LOG.debug(String.format("status = ok, responseMsg = %s", content));
+                LOG.debug("[HTTP-{}] response status = 200, {}", method, content);
             }
         }
 
@@ -194,58 +194,61 @@ public class ControllerAspect {
         response.setHeader("Trace-Id", ThreadLocalUtil.getTraceId());
         response.setHeader("cost", String.valueOf(System.currentTimeMillis() - startTime));
 
-        LOG.info(String.format("[response][%s] - %s, cost %sms", method, path, System.currentTimeMillis() - startTime));
+        if (LOG.isInfoEnabled()) {
+            LOG.info("[HTTP-ok] - {}, cost {} ms", path, System.currentTimeMillis() - startTime);
+        }
+
         return ret;
     }
 
     private String parseContent(Object obj) {
         if (obj == null) {
-            // content empty...
             return "";
         }
 
-        if (ObjectUtil.isBaseDataType(obj.getClass()) || obj instanceof String) {
-            return obj.toString();
+        Class clazz = obj.getClass();
+        if (ObjectUtil.isBaseDataType(clazz) || obj instanceof String) {
+            return this.cutStr(obj.toString());
         }
 
-        boolean jsonEnable = false;
-        if (obj instanceof Collection) {
-            jsonEnable = true;
-        } else if (obj instanceof Map) {
-            jsonEnable = true;
+        boolean canJson = false;
+        if (obj instanceof Collection || obj instanceof Map) {
+            canJson = true;
         } else if (obj instanceof Serializable) {
-            String classPath = obj.getClass().getName();
-
+            String classPath = clazz.getName();
             if (classPath.startsWith("org.springframework.data.domain.PageImpl")
                     || classPath.startsWith("cn.com.qmth")
                     || classPath.startsWith("com.qmth")) {
-                jsonEnable = true;
+                canJson = true;
             }
         }
 
-        if (!jsonEnable) {
-            // content ignore...
-            return "...";
+        if (!canJson) {
+            // can't to json
+            return clazz.getSimpleName();
         }
 
         String content = null;
         try {
-            // content to json
             content = new JsonMapper().toJson(obj);
         } catch (Exception e) {
-            LOG.warn("parseContent " + e.getMessage());
+            LOG.warn("parseContent fail! " + e.getMessage());
         }
 
-        if (content == null) {
-            return "......";
+        return this.cutStr(content);
+    }
+
+    private String cutStr(String str) {
+        if (str == null) {
+            return "";
         }
 
-        if (content.length() > logProperties.getResponseLogJsonMaxSize()) {
+        if (str.length() > logProperties.getResponseLogJsonMaxSize()) {
             // content too large...
-            return content.substring(0, logProperties.getResponseLogJsonMaxSize()) + " [MORE...]";
+            return str.substring(0, logProperties.getResponseLogJsonMaxSize()) + "......";
         }
 
-        return content;
+        return str;
     }
 
 }

+ 7 - 5
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/support/CustomExceptionHandler.java

@@ -235,20 +235,22 @@ public class CustomExceptionHandler {
             }
         }
 
-        String msg = String.format("[CustomExceptionHandler] status = %s, responseMsg = %s",
+        String msg = String.format("[%s] response status = %s, %s", request.getServletPath(),
                 httpStatus.value(), new JsonMapper().toJson(body));
+
         if (printStackTrace) {
             LOG.error(msg, err);
         } else {
-            LOG.error(msg + ", err is " + err.getMessage());
+            LOG.error("{}, Cause is {}", msg, err.getMessage());
 
             if (LOG.isDebugEnabled()) {
-                LOG.debug(err.getMessage(), err);
+                LOG.error(err.getMessage(), err);
             }
         }
+
         HttpHeaders headers = new HttpHeaders();
-        headers.add("Content-Type","application/json;charset=utf-8");
-        return new ResponseEntity<>(body, headers,httpStatus);
+        headers.add("Content-Type", "application/json;charset=utf-8");
+        return new ResponseEntity<>(body, headers, httpStatus);
     }
 
     /**