import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Objects;
@Aspect
@Configuration
@Slf4j
public class WebLogAspect {
/**
* 以自定义@WebLog注解为切点
*/
@Pointcut("@annotation(com.experiencetex.skills.config.annotation.WebLog)")
public void webLog() {
}
/**
* 环绕
*
* @return java.lang.Object
* @params [pjp]
* @description
*/
@Around("webLog()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = pjp.proceed();
if (!(result instanceof ResponseEntity)) {
log.info("Response Args : {}", new Gson().toJson(result));
log.info("Time-Consuming : {}", System.currentTimeMillis() - startTime);
}
return result;
}
/**
* Recorded before the tangent point
*
* @Return void
* @params [Joinpoint]
* @description
* /
@Before ( "Weblog ()" )
public void doBefore (the JoinPoint Joinpoint) throws Exception {
// Start print request log
ServletRequestAttributes Attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes ( );
the HttpServletRequest request = Objects.requireNonNull (Attributes) .getRequest ();
// Get the description of annotations @WebLog
String methodDescription = getAspectLogDescription (Joinpoint);
//Print request parameters
log.info ( "======================================== Start ========================================== " );
// print request url
log.info ( "the URL: {}" , request.getRequestURL () toString ().);
// print description
log.info ( "the description: {}" , methodDescription);
// Print Method the Http
log.info ( "method, the HTTP: {}" , request.getMethod ());
// print method of performing call controller and the full path of
log.info ( "Class method,:. {} {}" , joinPoint.getSignature (). getDeclaringTypeName () ., joinPoint.getSignature () getName ()
);// print request IP
log.info ( "the IP: {}" , request.getRemoteAddr ());
// print request into the reference
log.info ( "the Args the Request: {}", new new Gson () the toJson (joinPoint.getArgs ()).) ;
}
/ **
* Get the description section annotation
*
* @param Joinpoint tangent point
* @return description
* @throws Exception
* /
public String getAspectLogDescription (the JoinPoint Joinpoint)
throws Exception {
String the targetName = joinPoint.getTarget () getClass (. ) .getName ();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
StringBuilder description = new StringBuilder();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description.append(method.getAnnotation(WebLog.class).description());
break;
}
}
}
return description.toString();
}
}