代码测试,上线后的调试手段现在很多都喜欢用日志来处理,当我们需要查询某个接口报错的时候,直接看日志,问题描述不清晰,
会发现不知道前端传的什么参数,需要找前端确认,很耗时,所以加了一个请求入参和响应的拦截配置.
需要引入Spring的Aop相关的包:
org.springframework.boot
spring-boot-starter-aop
创建一个Controller层的切面拦截:
package com.test.logAop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
/**
* MVC 日志切面 记录
*
* @author Demon-HY
* @note spring-boot-devtools 与切面有冲突,导至在本地开发swagger-ui展示不出来
*/
@Aspect
public class LogAroundAop {
/**
* 在Controller 加日志切面
* 拦截 @RestController 注解的类,忽略 @LogIgnore 注解的类或接口
*/
@Pointcut(value = "((@within(org.springframework.web.bind.annotation.RestController))"
+ "||(@within(org.springframework.stereotype.Controller))"
+ ")")
public void logAround() {
}
// 请求进入前
@Before("logAround()")
public void doBefore(JoinPoint joinPoint) {
}
// 请求正常返回
@AfterReturning(value = "logAround()", returning = "result")
public void doAfterReturning(JoinPoint joinPoint, Object result) {
}
// 请求返回异常
@AfterThrowing(value = "logAround()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
}
}
所有注解了 @RestController和@Controller的类都可以被拦截到,里面有三个切面方法,分别是:
doBefore: 请求进入前拦截,记录请求日志
doAfterReturing:请求正常返回
doAfterThrowing:请求异常返回,这里可以拿到接口异常,但没办法处理异常,异常还是会抛给JVM,所有不要在里面使用try/catch
接下来我们在里面记录请求的入参和出参:
package com.test.logAop;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.xubei.framework.util.net.ServletRequestUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* MVC 日志切面 记录
*
* @author Demon-HY
* @note spring-boot-devtools 与切面有冲突,导至在本地开发swagger-ui展示不出来
*/
@Aspect
public class LogAroundAop {
private Logger logger = LoggerFactory.getLogger(this.getClass());
// mvc 出参打印的最大长度字符数
@Value("${server.mvc.print.return.limit:1024}")
private Integer retStrLimit;
// 记录请求时间
private static final ThreadLocal REQUEST_TIME = new ThreadLocal<>();
// 请求唯一标识
private static final ThreadLocal REQUEST_ID = new ThreadLocal<>();
private static void setRequestTime(Long requestTime) {
REQUEST_TIME.set(requestTime);
}
private static Long getRequestTime() {
return REQUEST_TIME.get();
}
private static void setRequestId() {
REQUEST_ID.set(UUID.randomUUID().toString().trim().replaceAll("-", "")
.substring(0, 12).toUpperCase());
}
private static String getRequestId() {
return REQUEST_ID.get();
}
// 清除本地线程的数据
private static void removeThreadLocal() {
REQUEST_TIME.remove();
REQUEST_ID.remove();
}
/**
* 在Controller 加日志切面,单个接口排除日志打印: {@link com.test.logAop.LogIgnore}注解
* 拦截 @RestController 注解的类,忽略 @LogIgnore 注解的类或接口
*/
@Pointcut(value = "((@within(org.springframework.web.bind.annotation.RestController))"
// + "||(@within(org.springframework.stereotype.Controller))"
// + "||(@annotation(org.springframework.web.bind.annotation.GetMapping))"
// + "||(@annotation(org.springframework.web.bind.annotation.PostMapping))"
// + "||(@annotation(org.springframework.web.bind.annotation.RequestMapping))"
+ ") && !(@within(com.test.logAop.LogIgnore))")
public void logAround() {
}
// 请求进入前
@Before("logAround()")
public void doBefore(JoinPoint joinPoint) {
// 记录请求时间
setRequestTime(System.currentTimeMillis());
// 记录一个请求的唯一ID,将该请求ID写入响应头,方便查找到该条日志
setRequestId();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest req = attributes.getRequest();
HttpServletResponse resp = attributes.getResponse();
// 请求的唯一标识,客户端通过这个可以查询到该次请求记录
resp.setHeader("RequestId", getRequestId());
// 处理完请求,返回内容
logger.info("REQ= IP:{} RequestId:{} Method:{} Uri:{} Header:{} Param:{}",
getIPAddr(req), getRequestId(), req.getMethod(), getRequestUrl(req),
getRequestHeader(req), getRequestParams(joinPoint));
}
// 请求正常返回
@AfterReturning(value = "logAround()", returning = "result")
public void doAfterReturning(JoinPoint joinPoint, Object result) {
try {
// 记录一个请求的唯一ID,将该请求ID写入响应头,方便查找到该条日志
String requestId = UUID.randomUUID().toString().trim().replaceAll("-", "")
.substring(0, 16).toUpperCase();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest req = attributes.getRequest();
HttpServletResponse resp = attributes.getResponse();
// 请求的唯一标识,客户端通过这个可以查询到该次请求记录
resp.setHeader("RequestId", requestId);
// 处理完请求,返回内容
logger.info("RESP= IP:{} RequestId:{} Method:{} Uri:{} Header:{} Param:{} Result:{} Time:{}",
getIPAddr(req), requestId, req.getMethod(), getRequestUrl(req), getRequestHeader(req),
getRequestParams(joinPoint), getResponseBody(result), getRequestTaking());
} finally {
removeThreadLocal();
}
}
// 请求返回异常
@AfterThrowing(value = "logAround()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest req = attributes.getRequest();
HttpServletResponse resp = attributes.getResponse();
// 请求的唯一标识,客户端通过这个可以查询到该次请求记录
resp.setHeader("RequestId", getRequestId());
// TODO 这里可以捕获异常,但无法处理异常,异常还是会抛给 JVM
// 处理完请求,返回内容
logger.error("RESP= IP:{} RequestId:{} Method:{} Uri:{} Header:{} Param:{} Error:{} Time:{}",
getIPAddr(req), getRequestId(), req.getMethod(), getRequestUrl(req), getRequestHeader(req),
getRequestParams(joinPoint), e.getMessage(), getRequestTaking(), e);
} finally {
removeThreadLocal();
}
}
// 获取请求路径
private String getRequestUrl(HttpServletRequest req) {
return req.getRequestURL().toString();
}
// 获取请求头
private Map> getRequestHeader(HttpServletRequest req) {
return ServletRequestUtil.getRequestHeaderMap(req);
}
// 获取请求参数
private Map getRequestParams(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
Map parameters=Maps.newLinkedHashMap();
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
//-parameters 设置带参数名编译
for (int i = 0; i < method.getParameters().length; i++) {
if(args[i] instanceof ServletRequest || args[i] instanceof ServletResponse){
continue;
}
parameters.put(method.getParameters()[i].getName(),args[i]);
}
return parameters;
}
// 获取返回结果
private String getResponseBody(Object result) {
String resultObj = "";
try {
resultObj = JSON.toJSONString(result);
resultObj = resultObj.length() > retStrLimit ? resultObj.substring(0, retStrLimit - 1) : resultObj;
} catch (Exception e) {
e.printStackTrace();
}
return resultObj;
}
// 获取请求耗时,单位毫秒
private Long getRequestTaking() {
Long endTime = System.currentTimeMillis();
return endTime - getRequestTime();
}
// 获取用户真实IP地址,不使用request.getRemoteAddr()的原因是有可能用户使用了代理软件方式避免真实IP地址,
// 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值
private static String getIPAddr(HttpServletRequest request) {
String ipAddress;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1") || ipAddress.equals("localhost")) {
// 根据网卡取本机配置的IP
InetAddress inet;
try {
inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress = "";
}
return ipAddress;
}
}
里面多了一个 LogIgnore注解,这个注解的作用是为了忽略掉不需要记录日志的请求,直接可以加在Controller层的类或方法上,
注解代码如下:
package com.test.logAop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 忽略日志切面的注解 {@link com.test.logAop.LogAroundAop}
*
* @author Demon-HY
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogIgnore {
String value() default "";
}
接下来我们需要把它注入到Spring容器中:
package com.test.logAop;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 注入 MVC 请求日志记录 Bean,需要在 application.properties 中配置 server.mvc.print.enabled=true,默认是开启的
*
* @author Demon-HY
*/
@Configuration
public class LogAopAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "server.mvc.print", name = "enabled", matchIfMissing = true)
public LogAroundAop logAroundAop() {
return new LogAroundAop();
}
}
接下来就可以在代码里面直接使用了