• 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);
    }
    }
    }
     
  • 相关阅读:
    【RL-TCPnet网络教程】第41章 HTTP超文本传输协议基础知识
    【安富莱TCPnet网络教程】HTTP通信实例
    【原创开源】网络版二代双通道示波器开源发布,支持电脑,手机和Pad等各种OS平台访问
    【RL-TCPnet网络教程】第40章 RL-TCPnet之TFTP客户端(精简版)
    【RL-TCPnet网络教程】第38章 TFTP简单文件传输基础知识
    linux添加环境变量
    linux字体安装
    lsof常用命令
    systemctl
    简单磁盘操作
  • 原文地址:https://www.cnblogs.com/lovetl/p/13891701.html
Copyright © 2020-2023  润新知