ea6a1250d99f22d33a5fb1d7e9585d359f03df94
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
6  #
7  # Licensed under the Apache License, Version 2.0 (the "License")
8  # you may not use this file except in compliance with the License.
9  # You may obtain a copy of the License at
10  #
11  #       http://www.apache.org/licenses/LICENSE-2.0
12  #
13  # Unless required by applicable law or agreed to in writing, software
14  # distributed under the License is distributed on an "AS IS" BASIS,
15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  # See the License for the specific language governing permissions and
17  # limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.adapters.nssmf.interceptor;
21
22 import org.aspectj.lang.ProceedingJoinPoint;
23 import org.aspectj.lang.annotation.Around;
24 import org.aspectj.lang.annotation.Aspect;
25 import org.aspectj.lang.annotation.Pointcut;
26 import org.aspectj.lang.reflect.MethodSignature;
27 import org.onap.so.adapters.nssmf.annotation.ServiceLogger;
28 import org.onap.so.adapters.nssmf.util.NssmfAdapterUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.core.annotation.Order;
32 import org.springframework.stereotype.Component;
33 import java.lang.reflect.Method;
34
35 /**
36  * support to print logger of service method
37  */
38 @Aspect
39 @Order(100)
40 @Component
41 public class LoggerInterceptor {
42
43     private static final Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);
44
45     @Pointcut("execution(* org.onap.so.adapters.nssmf.service..*(..))")
46     public void serviceLogger() {
47
48     }
49
50     @Around("serviceLogger()")
51     public Object around(ProceedingJoinPoint joinPoint) {
52         try {
53             MethodSignature signature = (MethodSignature) joinPoint.getSignature();
54             Method method = signature.getMethod();
55
56             Class<?> targetClass = method.getDeclaringClass();
57
58             StringBuilder classAndMethod = new StringBuilder();
59             ServiceLogger classAnnotation = targetClass.getAnnotation(ServiceLogger.class);
60             ServiceLogger methodAnnotation = method.getAnnotation(ServiceLogger.class);
61
62             if (classAnnotation == null && methodAnnotation == null) {
63                 return joinPoint.proceed();
64             }
65
66             if (classAnnotation != null) {
67                 if (classAnnotation.ignore()) {
68                     return joinPoint.proceed();
69                 }
70                 classAndMethod.append(classAnnotation.value()).append("-");
71             }
72
73             String target = targetClass.getName() + "#" + method.getName();
74
75             String params = NssmfAdapterUtil.marshal(joinPoint.getArgs());
76
77             logger.info("{} Start: Method = {} \nParams = {}", classAndMethod.toString(), target, params);
78
79             long start = System.currentTimeMillis();
80             Object result = joinPoint.proceed();
81             long timeConsuming = System.currentTimeMillis() - start;
82
83             logger.info("\n{} End: Method = {}, Spend time = {}ms \nResult = {}", classAndMethod.toString(), target,
84                     timeConsuming, NssmfAdapterUtil.marshal(result));
85             return result;
86
87         } catch (Throwable e) {
88             logger.error(e.getMessage(), e);
89         }
90         return null;
91     }
92 }