2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.aop;
23 import java.util.Arrays;
24 import java.util.Optional;
25 import lombok.SneakyThrows;
26 import lombok.extern.slf4j.Slf4j;
27 import org.aspectj.lang.ProceedingJoinPoint;
28 import org.aspectj.lang.annotation.Around;
29 import org.aspectj.lang.annotation.Aspect;
30 import org.aspectj.lang.reflect.MethodSignature;
31 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
32 import org.springframework.stereotype.Component;
33 import org.springframework.util.StopWatch;
38 @ConditionalOnExpression(
39 "'${logging.level.org.onap.cps}'.equalsIgnoreCase('DEBUG')"
41 public class CpsLoggingAspectService {
43 private static final String ALL_CPS_METHODS = "execution(* org.onap.cps..*(..)))";
46 * To measure method execution time as a logging.
47 * @param proceedingJoinPoint exposes the proceed(..) method in order to support around advice.
48 * @return empty in case of void otherwise an object of return type
50 @Around(ALL_CPS_METHODS)
52 public Object logMethodExecutionTime(final ProceedingJoinPoint proceedingJoinPoint) {
53 final StopWatch stopWatch = new StopWatch();
55 //Calculate method execution time
57 final Object logObject = Optional.ofNullable(proceedingJoinPoint.proceed()).orElse("");
59 final MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
60 //Log method execution time
61 log.debug("Execution time of : {}.{}() with argument[s] = {} having result = {} :: {} ms",
62 methodSignature.getDeclaringType().getSimpleName(),
63 methodSignature.getName(), Arrays.toString(proceedingJoinPoint.getArgs()), logObject,
64 stopWatch.getTotalTimeMillis());