springboot實現日誌記錄
阿新 • • 發佈:2020-10-28
首先開啟切面:
@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 { @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 = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { operateMethod = method.getAnnotation(ELog.class).operateMethod(); describe = method.getAnnotation(ELog.class).describe(); break; } } } //*========控制檯輸出=========*// log.info("=======================後置通知開始======================="); log.info("請求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()") + "." + operateMethod); log.info("方法描述:" + describe); 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); } } }