使用自定义注解打印接口出入参接口必要信息
自定义注解
打印日志注解
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;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
/**
* 日志打印时方法名
*/
String value();
/**
* 是否打印接口用时日志,默认打印
*/
boolean timeLog() default true;
/**
* 是否打印接口请求参数返回参数,默认打印
*/
boolean paramsLog() default true;
}
打印日志替换入参某字段值注解
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;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogValueReplace {
/**
* 表达式
* @return
*/
String expression();
/**
* 替换值
* @return
*/
String putValue() default "**";
}
使用AOP的方式是注解打印日志
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.test.boot.annotation.Log;
import com.test.boot.annotation.LogValueReplace;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Aspect
@Order(value = 100)
@Component
public class LogAop {
@Pointcut("@annotation(com.test.boot.annotation.Log)")
public void logPointcut() {
}
@Around("logPointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Class<?> clazz = joinPoint.getTarget().getClass();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = clazz.getMethod(signature.getName(), signature.getParameterTypes());
Log annotation = method.getAnnotation(Log.class);
String url = "";
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
url = "--url:" + requestAttributes.getRequest().getServletPath();
}
String name = annotation.value();
Object[] args = joinPoint.getArgs();
if (signature.getParameterTypes().length > 0 && annotation.paramsLog()) {
LogValueReplace logValueReplace = method.getAnnotation(LogValueReplace.class);
if (logValueReplace != null && StrUtil.isNotBlank(logValueReplace.expression())) {
JSON parse = JSONUtil.parse(args);
JSONUtil.putByPath(parse, logValueReplace.expression(), logValueReplace.putValue());
log.info(url + "&&&" + name + "输入参数:" + parse.toString());
} else {
String[] parameterNames = signature.getParameterNames();
String nameAndArgs = getNameAndArgs(args, parameterNames);
log.info(url + "***" + name + "输入参数:" + nameAndArgs);
}
}
Object proceed = joinPoint.proceed();
long stop = System.currentTimeMillis();
if (proceed != null && annotation.paramsLog()) {
log.info("***" + name + "返回参数:" + JSONObject.toJSONString(proceed));
}
if (annotation.timeLog()) {
log.info("@@@" + name + "用时" + (stop - start) + "ms");
}
return proceed;
}
public String getNameAndArgs(Object[] args, String[] parameterNames) {
Map<String, Object> param = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Object object = args[i];
if (object != null) {
if (object instanceof MultipartFile || object instanceof ServletRequest
|| object instanceof ServletResponse || object instanceof BeanPropertyBindingResult) {
continue;
}
param.put(parameterNames[i], object);
}
}
}
return JSONObject.toJSONString(param);
}
}
controller的写法
@PostMapping("/export/list/testUser")
@Log(value = "测试接口1111")
@LogValueReplace(expression = "[0].username")
public Person testUser(@RequestBody @Valid Person testUser) {
log.info("===================:" + JSONObject.toJSONString(testUser));
return testUser;
}
纸上得来终觉浅,绝知此事要躬行。