广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cn.huge.module.aop;
 
import cn.huge.base.common.utils.ObjectUtils;
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.springframework.stereotype.Component;
 
/**
 * @author zhouxiantao
 * @create 2024-08-30 11:46
 */
@Component
@Aspect
@Slf4j
public class LogAop {
    @Around("execution(* cn.huge.module.*.controller.*.*.*(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        long starttime = System.currentTimeMillis();
        Object[] objs = joinPoint.getArgs();
        try{
            log.info("start to {} {}", joinPoint.getSignature().getName(), JSON.toJSONString(objs));
        }catch (Exception e){
            log.info("start to {} 参数不能转成字符串,忽略不处理", joinPoint.getSignature().getName());
        }
        Object result = joinPoint.proceed(objs);
        long endtime = System.currentTimeMillis();
        try {
            String resultJson = JSON.toJSONString(result);
            if(ObjectUtils.isNotEmpty(result) && resultJson.length() > 1000) {
                log.info("finish to {} 日志过长不打印 {}ms", joinPoint.getSignature().getName(),endtime-starttime);
            }else {
                log.info("finish to {} {} {}ms", joinPoint.getSignature().getName(), resultJson,endtime-starttime);
            }
        }catch (Exception e){
            log.info("finish to {} 不能转成字符串,忽略不处理 {}ms", joinPoint.getSignature().getName(),endtime-starttime);
        }
        return result;
    }
 
    @Around("execution(* cn.huge.module.*.dao.mapper.*.*(..))")
    public Object aroundAdviceDao(ProceedingJoinPoint joinPoint) throws Throwable {
        long starttime = System.currentTimeMillis();
        Object[] objs = joinPoint.getArgs();
        Object result = joinPoint.proceed(objs);
        long endtime = System.currentTimeMillis();
        Long times = endtime-starttime;
        if(times > 2000){
            log.info("Executing sql consumes time more than 1s {} ,{}ms", joinPoint.getSignature().getName(),endtime-starttime);
        }else{
            log.info("Executing sql consumes time {} ,{}ms", joinPoint.getSignature().getName(),endtime-starttime);
        }
        return result;
    }
}