기존에 interceptor 로 로그를 출력하도록 소스를 짜놓았지만 리소스를 너무 잡아먹는 듯 하여

AOP에서 로그를 출력하도록 바꾸었다.

 

설명은 나중에 채워넣겠다.

 

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package ;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
 
 
@Component
@Aspect
public class ControllerAOP {
 
    protected static final Logger logger = LoggerFactory.getLogger(ControllerAOP.class);
 
    @Around("execution(public org.springframework.web.servlet.ModelAndView com.sample.bmp.mobile..*Controller.*(..))")
    public ModelAndView around(ProceedingJoinPoint pjp) {
        ModelAndView mav = null;
 
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
 
        Object inputParam = null;
        for (Object obj : pjp.getArgs()) {
            if (obj instanceof Map) {
                inputParam = obj;
            }
        }
 
        long start = System.currentTimeMillis();
 
        String controller = (String) pjp.getTarget().getClass().getSimpleName();
 
        String path = request.getRequestURI();
        String addr = request.getRemoteAddr();
        int port = request.getRemotePort();
 
        logger.info("##########################################################################");
        logger.info("# REQUEST | CONTROLLER = {} | METHOD = {} | REMOTEADDR = {} | PORT = {} | IN_PARAMS = {}",
                controller, path, addr, port, inputParam == null ? "": inputParam);
        logger.info("##########################################################################");
 
        Map<String, Object> result = null;
        
        try {
            mav = (ModelAndView) pjp.proceed();
            result = mav.getModel();
        } catch (APIException e) {
            mav = new ModelAndView("jsonView");
            result = new HashMap<String, Object>();
//            e.printStackTrace();
            result.put("result_cd", e.getErrCode());
            result.put("result_msg", e.getMessage());
 
            mav.addAllObjects(result);
        } catch (Throwable e) {
            mav = new ModelAndView("jsonView");
            result = new HashMap<String, Object>();
//            e.printStackTrace();
            result.put("result_cd"0);
            result.put("result_msg""");
 
            mav.addAllObjects(result);
        } finally {
            String success = "fail";
            
            if (result.containsKey("result_cd")) {
                try {
                    if ("1".equals(result.get("result_cd").toString())) {
                        success = "success";
                    }
                } catch (ClassCastException | NumberFormatException e) {
                    logger.debug("SessionInterceptor error == {}", e);
                }
            }
            
 
            long end = System.currentTimeMillis();
            logger.info("##########################################################################");
            logger.info("# RESPONSE | CONTROLLER = {} | METHOD = {} | RESULT = {} | REMOTEADDR = {} | PORT = {} | TIME = {} ms | IN_PARAMS = {} | OUT_PARAMS = {}",
                    controller, path, success, addr, port, end - start,
                    inputParam == null ? "" : inputParam, 
                    result == null ? "" : result.toString());
            logger.info("##########################################################################");
 
        }
 
        return mav;
    }
 
}
cs
반응형

+ Recent posts