添加方法日志切面
This commit is contained in:
parent
43c803080f
commit
cbd23f86b3
@ -6,13 +6,15 @@
|
|||||||
|
|
||||||
<groupId>cn.axzo.framework</groupId>
|
<groupId>cn.axzo.framework</groupId>
|
||||||
<artifactId>common-common</artifactId>
|
<artifactId>common-common</artifactId>
|
||||||
<version>1.0.3</version>
|
<version>1.0.4</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<!-- tool -->
|
<!-- tool -->
|
||||||
|
<aspectj.version>1.7.3</aspectj.version>
|
||||||
|
<aopalliance.version>1.0</aopalliance.version>
|
||||||
<lombok.version>1.18.18</lombok.version>
|
<lombok.version>1.18.18</lombok.version>
|
||||||
<commons-codec.version>1.8</commons-codec.version>
|
<commons-codec.version>1.8</commons-codec.version>
|
||||||
<slf4j-api.version>1.7.5</slf4j-api.version>
|
<slf4j-api.version>1.7.5</slf4j-api.version>
|
||||||
@ -29,6 +31,24 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjrt</artifactId>
|
||||||
|
<version>${aspectj.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>${aspectj.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>aopalliance</groupId>
|
||||||
|
<artifactId>aopalliance</artifactId>
|
||||||
|
<version>${aopalliance.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
|||||||
@ -0,0 +1,43 @@
|
|||||||
|
package cn.azxo.framework.common.annotation;
|
||||||
|
|
||||||
|
import cn.azxo.framework.common.annotation.support.UrlValidationValidator;
|
||||||
|
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
||||||
|
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||||
|
import static java.lang.annotation.ElementType.FIELD;
|
||||||
|
import static java.lang.annotation.ElementType.METHOD;
|
||||||
|
import static java.lang.annotation.ElementType.PARAMETER;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL 校验
|
||||||
|
*
|
||||||
|
* @author zhaoyong
|
||||||
|
* @see UrlValidation
|
||||||
|
* @since 2021-06-15 11:27
|
||||||
|
*/
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = UrlValidationValidator.class)
|
||||||
|
@Target({ METHOD, FIELD })
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface UrlValidation {
|
||||||
|
|
||||||
|
String message() default "mandatory field format is error";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
|
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@interface List {
|
||||||
|
UrlValidation[] value();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.azxo.framework.common.annotation.support;
|
||||||
|
|
||||||
|
import cn.azxo.framework.common.annotation.UrlValidation;
|
||||||
|
import cn.azxo.framework.common.utils.StringUtils;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UrlValidation 注解实现类
|
||||||
|
*
|
||||||
|
* @author zhaoyong
|
||||||
|
* @see UrlValidationValidator
|
||||||
|
* @since 2021-06-15 11:28
|
||||||
|
*/
|
||||||
|
public class UrlValidationValidator implements ConstraintValidator<UrlValidation, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(UrlValidation constraintAnnotation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
return StringUtils.isBlank(value) || value.startsWith("http://") || value.startsWith("https://");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package cn.azxo.framework.common.logger;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志注解
|
||||||
|
*
|
||||||
|
* @author zhaoyong
|
||||||
|
* @see MethodAroundLog
|
||||||
|
* @since 2021-06-17 18:00
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface MethodAroundLog {
|
||||||
|
|
||||||
|
String source() default "";
|
||||||
|
|
||||||
|
String target() default "";
|
||||||
|
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
package cn.azxo.framework.common.logger;
|
||||||
|
|
||||||
|
import cn.azxo.framework.common.utils.StringUtils;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.Signature;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志注解 @MethodAroundLog 实现类
|
||||||
|
*
|
||||||
|
* @author zhaoyong
|
||||||
|
* @see MethodAroundLogAspect
|
||||||
|
* @since 2021-06-17 18:01
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Slf4j
|
||||||
|
public class MethodAroundLogAspect {
|
||||||
|
|
||||||
|
@Around("@annotation(aroundLog)")
|
||||||
|
public Object methodHandler(ProceedingJoinPoint pjp, MethodAroundLog aroundLog) throws Throwable {
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
logRequest(pjp, aroundLog);
|
||||||
|
|
||||||
|
Object response = pjp.proceed();
|
||||||
|
|
||||||
|
logResponse(startTime, response, pjp, aroundLog);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logRequest(ProceedingJoinPoint pjp, MethodAroundLog aroundLog) {
|
||||||
|
String bizInfo = getBizInfo(aroundLog, true);
|
||||||
|
Object[] params = pjp.getArgs();
|
||||||
|
String signature = getSignature(pjp);
|
||||||
|
log.info("[{}] {} Begin request param is:{}", bizInfo ,signature, JSON.toJSONString(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logResponse(long startTime, Object response, ProceedingJoinPoint pjp, MethodAroundLog aroundLog) {
|
||||||
|
long endTime = System.currentTimeMillis();
|
||||||
|
String signatureInfo = getSignature(pjp);
|
||||||
|
String costTime = (endTime - startTime) + "ms";
|
||||||
|
String bizInfo = getBizInfo(aroundLog, false);
|
||||||
|
if (response != null) {
|
||||||
|
log.info("[{}] {} End response is : {},COST : {}", bizInfo, signatureInfo, JSON.toJSONString(response), costTime);
|
||||||
|
} else {
|
||||||
|
log.info("[{}] {} End response is null or void,COST : {}", bizInfo, signatureInfo, costTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getSignature(ProceedingJoinPoint pjp){
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
Signature signature = pjp.getSignature();
|
||||||
|
String typeName = signature.getDeclaringType().getSimpleName();
|
||||||
|
String methodName = signature.getName();
|
||||||
|
sb.append(typeName).append("#").append(methodName);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getBizInfo(MethodAroundLog aroundLog, boolean request){
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String source = aroundLog.source();
|
||||||
|
String target = aroundLog.target();
|
||||||
|
String value = aroundLog.value();
|
||||||
|
if(StringUtils.isNotBlank(source) && StringUtils.isNotBlank(target)) {
|
||||||
|
sb.append(source);
|
||||||
|
if(request) {
|
||||||
|
sb.append(" -> ");
|
||||||
|
} else {
|
||||||
|
sb.append(" <- ");
|
||||||
|
}
|
||||||
|
sb.append(target).append(":").append(value);
|
||||||
|
return sb.toString();
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user