|
@@ -1,30 +1,40 @@
|
|
package com.yx.face.boot.component.aop;
|
|
package com.yx.face.boot.component.aop;
|
|
-
|
|
|
|
-import com.yx.face.boot.uitls.JsonUtils;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+import org.aspectj.lang.Signature;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* <p>
|
|
* <p>
|
|
* 使用 aop 切面记录请求日志信息
|
|
* 使用 aop 切面记录请求日志信息
|
|
* </p>
|
|
* </p>
|
|
- * @description: 使用 aop 切面记录请求日志信息
|
|
|
|
*/
|
|
*/
|
|
-//@Aspect
|
|
|
|
-//@Component
|
|
|
|
|
|
+@Aspect
|
|
|
|
+@Component
|
|
@Slf4j
|
|
@Slf4j
|
|
public class ServiceLog {
|
|
public class ServiceLog {
|
|
|
|
+
|
|
|
|
+ private volatile static String logPrint = "less";
|
|
|
|
+
|
|
|
|
+ public static void updateLogPrint(String logPrint){
|
|
|
|
+ ServiceLog.logPrint = logPrint;
|
|
|
|
+ }
|
|
/**
|
|
/**
|
|
* 切入点
|
|
* 切入点
|
|
*/
|
|
*/
|
|
- @Pointcut("execution(public * com.yx.face.service.impl.*ServiceImpl.*(..))")
|
|
|
|
|
|
+ @Pointcut("execution(public * com.yx.face.service.impl.*.*(..))")
|
|
public void log() {
|
|
public void log() {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 环绕操作
|
|
* 环绕操作
|
|
*
|
|
*
|
|
@@ -34,14 +44,59 @@ public class ServiceLog {
|
|
*/
|
|
*/
|
|
@Around("log()")
|
|
@Around("log()")
|
|
public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
|
|
public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
|
|
- log.info("【请求类名】:{}", point.getSignature().getDeclaringTypeName());
|
|
|
|
- log.info("【请求方法名】:{}【参数】:{}", point.getSignature().getName(), point.getArgs());
|
|
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
|
|
+ Signature signature = point.getSignature();
|
|
|
|
+ MyAnnotation annotation = getAnnotation(point);
|
|
|
|
+ if(annotation == null){
|
|
|
|
+ log.info("【请求日志级别】:{}【请求类名】:{}【请求方法名】:{}",logPrint,signature.getDeclaringTypeName(),signature.getName());
|
|
|
|
+ try {
|
|
|
|
+ String req = JSON.toJSONString(point.getArgs());
|
|
|
|
+ log.info("【参数】:{}",this.logPrint(req));//如果传的是流就会报错
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ log.info("打印参数报错");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //运行
|
|
Object result = point.proceed();
|
|
Object result = point.proceed();
|
|
- log.info("【耗时】:{}毫秒", System.currentTimeMillis() - start);
|
|
|
|
- //log.info("【返回值】:{}", JsonUtils.toJson(result));
|
|
|
|
|
|
+ if(annotation == null){
|
|
|
|
+ try {
|
|
|
|
+ String resp = JSON.toJSONString(result);
|
|
|
|
+ log.info("【返回值】:{}",this.logPrint(resp));//如果返回的是流就会报错
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ log.info("打印返回值报错");
|
|
|
|
+ }
|
|
|
|
+ log.info("【耗时】:{}",System.currentTimeMillis() - start);
|
|
|
|
+ }
|
|
|
|
+
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private String logPrint(String req) {
|
|
|
|
+ String value = logPrint;
|
|
|
|
+ switch (value){
|
|
|
|
+ case "all" : return req;
|
|
|
|
+ case "less" : return this.getThousand(req);
|
|
|
|
+ default:return this.getThousand(req);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getThousand(String req) {
|
|
|
|
+ if(req.length() > 1000){
|
|
|
|
+ return req.substring(0,1000);
|
|
|
|
+ }
|
|
|
|
+ return req;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private MyAnnotation getAnnotation(ProceedingJoinPoint pjp) {
|
|
|
|
+ MethodSignature ms = (MethodSignature) pjp.getSignature();
|
|
|
|
+ Object target = pjp.getTarget();
|
|
|
|
+ Method method1 = null;
|
|
|
|
+ try {
|
|
|
|
+ method1 = target.getClass().getMethod(ms.getName(), ms.getParameterTypes());
|
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return method1 == null ? null : method1.getAnnotation(MyAnnotation.class);
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|