org.shoulder.web.advice.RestControllerJsonLogAspect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shoulder-web Show documentation
Show all versions of shoulder-web Show documentation
shoulder WEB 模块,基于Spring Boot Web提供了 Controller AOP 日志、AOP异常处理,统一返回值,健康检查,租户、用户解析,Web
安全防护,通用CrudController,动态字典,标签管理,HTTP client AOP日志、AOP异常处理等能力,助力Web飞速开发。
The newest version!
package org.shoulder.web.advice;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.shoulder.core.log.AppLoggers;
import org.shoulder.core.log.Logger;
import org.shoulder.core.util.JsonUtils;
import org.shoulder.core.util.ServletUtil;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* 生产环境接口入参默认,以 Json 形式记录接口出入参数
*
* @author lym
*/
public class RestControllerJsonLogAspect extends BaseRestControllerLogAspect {
public RestControllerJsonLogAspect(boolean useControllerLogger) {
super(useControllerLogger);
}
/**
* 前置方法较长,单独抽出以保证 JIT 优化
*
* @param jp 连接点
* @param log logger
*/
@Override
protected void before(JoinPoint jp, Logger log) {
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Method method = methodSignature.getMethod();
// 记录请求方法、路径,Controller 信息与代码位置
HttpServletRequest request = ServletUtil.getRequest();
StringBuilder requestInfo = new StringBuilder(request.getMethod()).append(" ")
.append(request.getRequestURI());
Parameter[] parameters = method.getParameters();
String[] parameterNames = methodSignature.getParameterNames();
requestInfo.append(" HEADER: ");
Enumeration headerNames = request.getHeaderNames();
Map headers = new HashMap<>();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headers.put(headerName, headerValue);
/*requestInfo
.append(headerName)
.append(": ")
.append(headerValue)
.append(" ");*/
}
requestInfo.append(JsonUtils.toJson(headers));
Object[] args = jp.getArgs();
// 记录 Controller 入参
if (parameters.length > 0) {
requestInfo.append(" PARAMS: ");
}
Map argsMap = new HashMap<>(parameters.length);
for (int i = 0; i < parameters.length; i++) {
String argName = parameterNames[i];
String argValue = RestControllerColorfulLogAspect.toLogValue(args[i]);
// 过长的参数可能导致缓慢
argsMap.put(argName, argValue);
/*requestInfo
.append(argType.getSimpleName())
.append(" ")
.append(argName)
.append(": ")
.append(argValue);*/
}
requestInfo.append(JsonUtils.toJson(argsMap));
log.info(requestInfo.toString());
}
@Override
protected void after(ProceedingJoinPoint jp, Logger log, Object returnObject, long cost) {
String requestUrl = ServletUtil.getRequest().getRequestURI();
String returnStr = returnObject != null ? JsonUtils.toJson(returnObject) : "null";
log.info("url={}, success={}, result={}", requestUrl, true, returnStr);
AppLoggers.APP_SERVICE_DIGEST.info("url={}, success={}, cost={}, result={}", requestUrl, true, cost, returnStr);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy