admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
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
58
59
60
61
62
package com.yeshi.buwan.aspect;
 
import com.yeshi.buwan.log.LogHelper;
import net.sf.ehcache.CacheManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
@Component
@Aspect()
public class LogAspect {
    private final Logger logger = LoggerFactory.getLogger(LogAspect.class);
 
    private String getMethodName(ProceedingJoinPoint pjp) {
        try {
            MethodSignature sig = (MethodSignature) pjp.getSignature();
            Method method = sig.getMethod();
            return method.getDeclaringClass().getName() + "." + sig.getMethod().getName();
        } catch (Exception e) {
            return "";
        }
    }
 
    @Around("execution(public * com.yeshi.buwan.controller.parser.*.*(..))")
    public Object countTime(ProceedingJoinPoint joinPoint) {
 
        Object[] args = joinPoint.getArgs();
        long starttime = System.currentTimeMillis();
        Object obj = null;
        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            logger.error("客户端接口访问出错:", e);
        }
        LogHelper.countTime(getMethodName(joinPoint) + "#" + (System.currentTimeMillis() - starttime) + "");
        return obj;
    }
 
    // @Around("execution(public * com.yeshi.buwan.controller.parser.*(..))")
    public Object statisticEhcache(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        long starttime = System.currentTimeMillis();
        Object obj = null;
        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        LogHelper.countTime(getMethodName(joinPoint));
        CacheManager manager = CacheManager.newInstance("src/ehcache.xml");
        // manager.
        // Cache test = manager.getCache("testCache");
        return obj;
    }
 
}