<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
代码:
package com.example.testutils.aop;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.stream.Collectors;
@Slf4j
@Aspect
@Component
public class ControllerLogAspect {
@Pointcut("execution(public * com.example.testutils..controllers..*(..))")
private void controllerLog(){}
@Before("controllerLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("=============== Start ===============");
log.info("URL : {}", request.getRequestURL().toString());
String requset = Arrays.toString(joinPoint.getArgs());
if (requset.length() > 1000) {
requset = requset.substring(0, 1000) + "...";
}
String requestData = request.getReader().lines().collect(Collectors.joining()).replaceAll(" ","");
log.info("Request Args : {}", JSON.toJSONString(requset));
log.info("Request Args : {}", requestData);
}
@After("controllerLog()")
public void doAfter(JoinPoint joinPoint) throws Throwable {
log.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName());
log.info("=============== End ===============");
log.info("");
}
@Around("controllerLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
long startTime = System.currentTimeMillis();
Object result;
try {
result = proceedingJoinPoint.proceed();
} catch (Throwable t) {
long end = System.currentTimeMillis();
long takeTime = end - startTime;
log.info(request.getRequestURL().toString() + " 执行失败 执行耗时:{} ms,errorMsg:{}", takeTime, t.getMessage());
throw t;
}
String response = JSON.toJSONString(result);
if (response.length() > 1000) {
response = response.substring(0, 1000) + "...";
}
long end = System.currentTimeMillis();
long takeTime = end - startTime;
log.info(request.getRequestURL().toString() + " 执行成功 执行耗时:{} ms,ResponseUtil: {}", takeTime, response);
return result;
}
}
|