|
|
@@ -1,5 +1,7 @@ |
|
|
package com.iformall.interceptor; |
|
|
package com.iformall.interceptor; |
|
|
|
|
|
|
|
|
|
|
|
import com.iformall.utils.IPUtil; |
|
|
|
|
|
import lombok.Data; |
|
|
import org.aspectj.lang.JoinPoint; |
|
|
import org.aspectj.lang.JoinPoint; |
|
|
import org.aspectj.lang.annotation.AfterReturning; |
|
|
import org.aspectj.lang.annotation.AfterReturning; |
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
@@ -19,7 +21,16 @@ import java.util.Arrays; |
|
|
public class WebLogAspect { |
|
|
public class WebLogAspect { |
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
|
|
|
|
|
|
ThreadLocal<Long> startTime = new ThreadLocal<>(); |
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
private class MethodInfo { |
|
|
|
|
|
Long startTime; |
|
|
|
|
|
String url; |
|
|
|
|
|
String ip; |
|
|
|
|
|
String method; |
|
|
|
|
|
String classMethod; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ThreadLocal<MethodInfo> startInfo = new ThreadLocal<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Pointcut("execution(public * com.iformall.controller..*.*(..))") |
|
|
@Pointcut("execution(public * com.iformall.controller..*.*(..))") |
|
|
@@ -28,18 +39,17 @@ public class WebLogAspect { |
|
|
@Before("webLog()") |
|
|
@Before("webLog()") |
|
|
public void doBefore(JoinPoint joinPoint) throws Throwable { |
|
|
public void doBefore(JoinPoint joinPoint) throws Throwable { |
|
|
logger.debug("aspect start"); |
|
|
logger.debug("aspect start"); |
|
|
startTime.set(System.currentTimeMillis()); |
|
|
|
|
|
|
|
|
|
|
|
// 接收到请求,记录请求内容 |
|
|
// 接收到请求,记录请求内容 |
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
|
HttpServletRequest request = attributes.getRequest(); |
|
|
HttpServletRequest request = attributes.getRequest(); |
|
|
|
|
|
|
|
|
// 记录下请求内容 |
|
|
|
|
|
logger.info("URL : " + request.getRequestURL().toString()); |
|
|
|
|
|
logger.info("HTTP_METHOD : " + request.getMethod()); |
|
|
|
|
|
logger.info("IP : " + request.getRemoteAddr()); |
|
|
|
|
|
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); |
|
|
|
|
|
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); |
|
|
|
|
|
|
|
|
MethodInfo info = new MethodInfo(); |
|
|
|
|
|
info.setUrl(request.getRequestURL().toString()); |
|
|
|
|
|
info.setStartTime(System.currentTimeMillis()); |
|
|
|
|
|
info.setIp(IPUtil.getIpAddr(request)); |
|
|
|
|
|
info.setMethod(request.getMethod()); |
|
|
|
|
|
info.setClassMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); |
|
|
|
|
|
startInfo.set(info); |
|
|
logger.debug("aspect start ..."); |
|
|
logger.debug("aspect start ..."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -47,8 +57,14 @@ public class WebLogAspect { |
|
|
public void doAfterReturning(Object ret) throws Throwable { |
|
|
public void doAfterReturning(Object ret) throws Throwable { |
|
|
logger.debug("aspect after"); |
|
|
logger.debug("aspect after"); |
|
|
// 处理完请求,返回内容 |
|
|
// 处理完请求,返回内容 |
|
|
logger.info("RESPONSE : " + ret); |
|
|
|
|
|
logger.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get())); |
|
|
|
|
|
|
|
|
MethodInfo info = startInfo.get(); |
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
sb.append("URL: ").append(info.getUrl()) |
|
|
|
|
|
.append(", METHOD: ").append(info.getMethod()) |
|
|
|
|
|
.append(", DO: ").append(info.getClassMethod()) |
|
|
|
|
|
.append(", IP: ").append(info.getIp()) |
|
|
|
|
|
.append(", SPEND TIME: ").append(System.currentTimeMillis() -info.getStartTime()).append("ms"); |
|
|
|
|
|
logger.info(sb.toString()); |
|
|
logger.debug("aspect after .."); |
|
|
logger.debug("aspect after .."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|