From 961b63fe16703900581cb2f2c77ef79f6cc37ab9 Mon Sep 17 00:00:00 2001 From: Lathish Date: Thu, 19 Mar 2020 15:24:32 +0000 Subject: [PATCH] Spring Aspect logging Issue-ID: NONRTRIC-170 Change-Id: I297c19fbb66de9abd6a62b115561c092368f0391 Signed-off-by: Lathish --- policy-agent/config/application.yaml | 4 +- policy-agent/pom.xml | 4 ++ .../org/oransc/policyagent/aspect/LogAspect.java | 63 ++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/aspect/LogAspect.java diff --git a/policy-agent/config/application.yaml b/policy-agent/config/application.yaml index 9fb3fba5..f0fa3a0a 100644 --- a/policy-agent/config/application.yaml +++ b/policy-agent/config/application.yaml @@ -2,7 +2,9 @@ spring: profiles: active: prod main: - allow-bean-definition-overriding: true + allow-bean-definition-overriding: true + aop: + auto: false management: endpoints: web: diff --git a/policy-agent/pom.xml b/policy-agent/pom.xml index b8dcbc47..f718d0ba 100644 --- a/policy-agent/pom.xml +++ b/policy-agent/pom.xml @@ -75,6 +75,10 @@ org.springframework.boot spring-boot-starter-webflux + + org.springframework.boot + spring-boot-starter-aop + org.springframework.boot spring-boot-devtools diff --git a/policy-agent/src/main/java/org/oransc/policyagent/aspect/LogAspect.java b/policy-agent/src/main/java/org/oransc/policyagent/aspect/LogAspect.java new file mode 100644 index 00000000..f4419b76 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/aspect/LogAspect.java @@ -0,0 +1,63 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2020 Nordix Foundation + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.oransc.policyagent.aspect; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.util.StopWatch; + +@Aspect +@Component +public class LogAspect { + + private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); + + @Around("execution(* org.oransc.policyagent..*(..)))") + public void executimeTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); + String className = methodSignature.getDeclaringType().getSimpleName(); + String methodName = methodSignature.getName(); + final StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + proceedingJoinPoint.proceed(); + stopWatch.stop(); + logger.trace("Execution time of {}.{}: {} ms", className, methodName, stopWatch.getTotalTimeMillis()); + } + + @Before("execution(* org.oransc.policyagent..*(..)))") + public void entryLog(final JoinPoint joinPoint) throws Throwable { + logger.trace("Entering method: {}", joinPoint.getSignature().getName()); + } + + @After("execution(* org.oransc.policyagent..*(..)))") + public void exitLog(final JoinPoint joinPoint) throws Throwable { + logger.trace("Exiting method: {}", joinPoint.getSignature().getName()); + } + +} -- 2.16.6