feat: 配置日志切面

This commit is contained in:
pnoker
2022-03-13 23:22:18 +08:00
parent 38855213f6
commit 6b45fad2f6
4 changed files with 75 additions and 1 deletions
@@ -0,0 +1,22 @@
package com.dc3.common.annotation;
import java.lang.annotation.*;
/**
* 日志切点
*
* @author pnoker
*/
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Logs {
String value() default "";
LogsType type() default LogsType.INFO;
String tag() default "";
boolean save() default false;
}
@@ -0,0 +1,41 @@
package com.dc3.common.annotation;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 日志切片
*
* @author pnoker
*/
@Slf4j
@Aspect
@Component
public class LogsAspect {
@Pointcut("@annotation(Logs)")
public void logsCut() {
}
@Around("logsCut()&&@annotation(logs)")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint, Logs logs) throws Throwable {
String uuid = IdUtil.fastSimpleUUID();
String className = proceedingJoinPoint.getSignature().getDeclaringType().getSimpleName();
String methodName = proceedingJoinPoint.getSignature().getName();
long startTime = System.currentTimeMillis();
log.info("Start => [{}].[{}.{}]: {}", uuid, className, methodName, logs.value());
try {
Object proceed = proceedingJoinPoint.proceed();
log.info("End <= [{}].[{}.{}].[{}ms]: {}", uuid, className, methodName, System.currentTimeMillis() - startTime, logs.value());
return proceed;
} catch (Throwable throwable) {
log.info("End <= [{}].[{}.{}].[{}ms]: {}", uuid, className, methodName, System.currentTimeMillis() - startTime, logs.value());
throw throwable;
}
}
}
@@ -0,0 +1,10 @@
package com.dc3.common.annotation;
/**
* 日志类型
*
* @author pnoker
*/
public enum LogsType {
INFO, ERROR;
}
@@ -4,4 +4,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dc3.common.config.RedisTemplateConfig,\
com.dc3.common.config.RedisCacheConfig,\
com.dc3.common.config.ThreadPoolConfig,\
com.dc3.common.utils.RedisUtil
com.dc3.common.utils.RedisUtil,\
com.dc3.common.annotation.LogsAspect