2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.so.adapters.nssmf.interceptor;
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;
36 * support to print logger of service method
41 public class LoggerInterceptor {
43 private static final Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);
45 @Pointcut("execution(* org.onap.so.adapters.nssmf.service..*(..))")
46 public void serviceLogger() {
50 @Around("serviceLogger()")
51 public Object around(ProceedingJoinPoint joinPoint) {
53 MethodSignature signature = (MethodSignature) joinPoint.getSignature();
54 Method method = signature.getMethod();
56 Class<?> targetClass = method.getDeclaringClass();
58 StringBuilder classAndMethod = new StringBuilder();
59 ServiceLogger classAnnotation = targetClass.getAnnotation(ServiceLogger.class);
60 ServiceLogger methodAnnotation = method.getAnnotation(ServiceLogger.class);
62 if (classAnnotation == null && methodAnnotation == null) {
63 return joinPoint.proceed();
66 if (classAnnotation != null) {
67 if (classAnnotation.ignore()) {
68 return joinPoint.proceed();
70 classAndMethod.append(classAnnotation.value()).append("-");
73 String target = targetClass.getName() + "#" + method.getName();
75 String params = NssmfAdapterUtil.marshal(joinPoint.getArgs());
77 logger.info("{} Start: Method = {} \nParams = {}", classAndMethod.toString(), target, params);
79 long start = System.currentTimeMillis();
80 Object result = joinPoint.proceed();
81 long timeConsuming = System.currentTimeMillis() - start;
83 logger.info("\n{} End: Method = {}, Spend time = {}ms \nResult = {}", classAndMethod.toString(), target,
84 timeConsuming, NssmfAdapterUtil.marshal(result));
87 } catch (Throwable e) {
88 logger.error(e.getMessage(), e);