springboot实现日志记录

首先开启切面:
@SpringBootApplication
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
然后pom.xml引入依赖
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
日志注解类
import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ELog {
    String describe() default "";
    String operateMethod() default "";
}
切面类
import com.example.demo.annotation.ELog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author xulei
 * @version 1.0
 * @date 2020/10/28 14:43
 */
@Slf4j
@Aspect
@Component
public class ElogAspect {

private Class clazz;

 
@Pointcut("execution (* com.example.demo.controller..*.*(..))")
public void controllerAspect() {
}

@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) {
log.info("开始调用接口" + joinPoint);
clazz = joinPoint.getTarget().getClass();
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
log.info("=======================前置通知 开始=======================");

log.info("请求IP:" + request.getRemoteAddr());
log.info("请求地址:" + request.getRequestURI());
log.info("请求方式:" + request.getMethod());
log.info("请求类方法:" + joinPoint.getSignature());
log.info("请求类方法参数:" + Arrays.toString(joinPoint.getArgs()));
String url = "";
log.info("=======================前置通知 结束=======================");
}

@After("controllerAspect()")
public void after(JoinPoint joinPoint) {
try {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = null;

targetClass = Class.forName(targetName);

Method[] methods = targetClass.getMethods();
String operateMethod = "";
String describe = "";
String object = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
try {
operateMethod = joinPoint.getTarget().getClass().getDeclaredMethod(method.getName(),method.getParameterTypes()).getAnnotation(ELog.class).operateMethod();
describe = joinPoint.getTarget().getClass().getDeclaredMethod(method.getName(),method.getParameterTypes()).getAnnotation(ELog.class).describe();
object = joinPoint.getTarget().getClass().getDeclaredMethod(method.getName(),method.getParameterTypes()).getAnnotation(ELog.class).obj();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
break;
}

}

}

//*========控制台输出=========*//
log.info("=======================后置通知开始=======================");
log.info("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()") + "." + operateMethod);
log.info("方法描述:" + describe);
log.info("操作的对象" + object);
log.info("=======================后置通知结束=======================");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

private void url(String[] classValue, boolean b, String[] value) {
String url;
if (b) {
String[] methodValue = value;
url = classValue[0] + methodValue[0];
log.info("url:{}", url);
}
}
}
 
原文地址:https://www.cnblogs.com/lovetl/p/13891701.html