Spring AOP method execution time and Environment Variable controlled logging

implementation( group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: '2.5.7')
package foo.bar;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

@Aspect
@Component
@ConditionalOnExpression("#{systemEnvironment['AOP_LOG_METHOD_TIMING'] == 'true'}") // Optional if you wish to control
public class Profile {


private static final Logger LOGGER = LogManager.getLogger(Profile.class);
@Around("execution(* foo.bar..*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
final long start = System.currentTimeMillis();

final Object proceed = joinPoint.proceed();

final long executionTime = System.currentTimeMillis() - start;

LOGGER.info("ExecutionTime {}: {} [ms]\n",
joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName(), executionTime);

return proceed;
}
}
@ConditionalOnExpression("#{systemEnvironment['AOP_LOG_METHOD_TIMING'] == 'true'}")
public class Profile {


private static final Logger LOGGER = LogManager.getLogger(Profile.class);
@Around("execution(* foo.bar..*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
final long start = System.currentTimeMillis();

final Object proceed = joinPoint.proceed();

final long executionTime = System.currentTimeMillis() - start;

LOGGER.info("ExecutionTime {}: {} [ms]\n",
joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName(), executionTime);

return proceed;
}
}

--

--

--

Tech Enthusiast | Programmer | Architect | Likes to Travel | Interests in Technology, History, Languages, Science

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Happens When You Type ls -l *.c And Hit Enter In A Shell

DevOps POV: Behind the Kubernetes wheel

Google Storytime: How to Engineer a Promo

A Small Matrix Calculator with C++

OOP Pillars

Resource API

Received notifications in Slack when a change happen on a GitHub repository or organization account

A Pragmatic Approach to System Design

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Basavaraj V

Basavaraj V

Tech Enthusiast | Programmer | Architect | Likes to Travel | Interests in Technology, History, Languages, Science

More from Medium

Evolve Database Using Liquibase

Add Auth0 authentication to a Java application using Datawiza in 5 mins

Authorisation with Spring Security — Part 1

Load Balancing, API Gateway, Service Discovery and Monitoring in Microservices using Ribbon, Spring…