Springboot 日志切面类 代码示例

JDK版本
1.8+

POM依赖

        <!-- AOP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <!-- fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        

代码


import com.alibaba.fastjson.JSON;
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.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Aspect
@Component
public class LogAspect {


    @Pointcut("execution(* com.lh.controller..*.*(..))")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object SystemLog(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //请求的 类名、方法名
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = signature.getName();
        //请求的参数
        Object[] args = joinPoint.getArgs();
        List<Object> list = new ArrayList<>();
        try {
            list.addAll(Arrays.asList(args));
        } catch (Exception ignored) {
        }

        log.info("[LogAspect before] className : {}, methodName : {}, params: {} ",
                className, methodName, JSON.toJSONString(list));
        LocalDateTime start = LocalDateTime.now();
        Object res = joinPoint.proceed();
        Duration between = Duration.between(start, LocalDateTime.now());

        log.info("[LogAspect after] methodName : {},  use time(Millis) : {}", methodName, between.toMillis());
        return res;
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 提供了很好的支持来实现面向切面编程。面向切面编程(AOP)是一种编程范式,它允许将横跨多个业务逻辑的功能(例如日志记录、事务管理等)从应用程序的核心业务逻辑中分离出来,以提高代码的可重用性、模块化和可维护性。 在 Spring Boot 中,你可以使用 Spring AOP 来实现面向切面编程。Spring AOP 基于代理模式,通过在目标对象上动态生成一个代理对象来实现切面功能。Spring AOP 提供了很多注解来定义切面、切入点、通知等。 下面是一个简单的面向切面编程的示例,它演示了如何在 Spring Boot 中使用 AOP 记录方法的执行时间: 1. 定义一个切面,使用 @Aspect 注解标记,定义一个切入点,使用 @Pointcut 注解标记: ``` @Aspect @Component public class LogAspect { @Pointcut("execution(* com.example.demo.service.*.*(..))") public void servicePointcut() { } @Around("servicePointcut()") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object proceed = joinPoint.proceed(); System.out.println(joinPoint.getSignature().getName() + " executed in " + (System.currentTimeMillis() - startTime) + "ms"); return proceed; } } ``` 2. 在应用程序的启动上添加 @EnableAspectJAutoProxy 注解来启用 AOP: ``` @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,在调用 com.example.demo.service 包下的任何方法时,LogAspect 中定义的 logExecutionTime 方法都会被调用,记录方法的执行时间。 以上是一个简单的面向切面编程示例。在实际应用中,你可以使用 AOP 来实现很多功能,例如事务管理、安全控制等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值