切面方法添加日志
自定义日志注解
package com.wmang.scan.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogTime {
public static final String INFO = "info";
public static final String DEBUG = "debug";
String level() default INFO;
String profix() default "";
}
切面方法实现
package com.wmang.scan.aspect;
import com.alibaba.fastjson.JSON;
import com.wmang.scan.annotation.LogTime;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Aspect
@Component
@Slf4j
public class LogAspect {
private static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private ThreadLocal<Loggers> loggers = new ThreadLocal<>();
@Pointcut("execution(public * com.wmang.scan.*.controller..*(..))")
public void logger() {
}
@Before(value = "logger()")
public void doBefore(JoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
String className = point.getTarget().getClass().getName();
String methodName = method.getName();
Object[] args = point.getArgs();
Loggers lger = new Loggers();
lger.setClassName(className);
lger.setMethodName(methodName);
lger.setArgs(args);
lger.setStart(LocalDateTime.now());
loggers.set(lger);
}
@AfterReturning(value = "logger()", returning = "result")
public void doAferReturning(Object result) {
Loggers lger = loggers.get();
lger.setResult(result);
lger.setEnd(LocalDateTime.now());
log.debug("{}", lger.toString());
}
@Data
private class Loggers {
private LocalDateTime start;
private LocalDateTime end;
private String className;
private String methodName;
private Object[] args;
private Object result;
@Override
public String toString() {
StringBuilder sb = new StringBuilder("日志详情:\n\t\t类名:");
sb.append(className);
sb.append("\n\t\t方法名:");
sb.append(methodName);
sb.append("\n\t\t入参:");
sb.append(JSON.toJSONString(args));
sb.append("\n\t\t开始执行时间:");
sb.append(start.format(LogAspect.df));
sb.append("\n\t\t结束执行时间:");
sb.append(end.format(LogAspect.df));
sb.append("\n\t\t执行结果:");
sb.append(JSON.toJSONString(result));
return sb.toString();
}
}
@Pointcut(value = "@annotation(com.wmang.scan.annotation.LogTime)")
public void aspectLog() {
}
@Around(value = "aspectLog()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Object[] args = point.getArgs();
Class classz = point.getTarget().getClass();
Method method = signature.getMethod();
Loggers loggers = new Loggers();
loggers.setClassName(classz.getName());
loggers.setMethodName(method.getName());
loggers.setArgs(args);
LogTime clzLogTime = (LogTime) classz.getAnnotation(LogTime.class);
LogTime mthLogTime = method.getAnnotation(LogTime.class);
String profix = StringUtils.isNotBlank(mthLogTime.profix()) ? mthLogTime.profix():clzLogTime.profix();
StopWatch stopWatch = new StopWatch();
loggers.setStart(LocalDateTime.now());
stopWatch.start();
Object result = point.proceed();
stopWatch.stop();
loggers.setEnd(LocalDateTime.now());
loggers.setResult(result);
if (mthLogTime.level().equalsIgnoreCase(LogTime.INFO)) {
log.info("profix{},\n\t\t总共耗时【{}】毫秒", loggers.toString(), stopWatch.getTime());
}
if (mthLogTime.level().equalsIgnoreCase(LogTime.DEBUG)) {
log.debug("{},\n\t\t总共耗时【{}】毫秒", loggers.toString(), stopWatch.getTime());
}
return result;
}
}