Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Last active February 5, 2024 08:53
Show Gist options
  • Save aoudiamoncef/5845420f1fa019580d31c0daa9d7493f to your computer and use it in GitHub Desktop.
Save aoudiamoncef/5845420f1fa019580d31c0daa9d7493f to your computer and use it in GitHub Desktop.
Spring Boot with Spring AOP Logging (Use for debugging only)
package com.maoudia.app;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
public class LoggingAspect {
private final Logger log = LoggerFactory.getLogger(LoggingAspect.class);
@Before("execution(* com.maoudia.app..*(..))")
public void logBeforeMethodExecution(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().toShortString();
String args = Arrays.toString(joinPoint.getArgs());
log.debug("Executing method: {} with arguments: {}", methodName, args);
}
@AfterReturning(
pointcut = "execution(* com.maoudia.app..*(..))",
returning = "result")
public void logAfterMethodExecution(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().toShortString();
log.debug("Method: {} returned: {}", methodName, result);
}
@AfterThrowing(
pointcut = "execution(* com.maoudia.app..*(..))",
throwing = "exception")
public void logOnException(JoinPoint joinPoint, Exception exception) {
String methodName = joinPoint.getSignature().toShortString();
log.debug("Method: {} threw exception: {}", methodName, exception.getMessage());
}
@After("execution(* com.maoudia.app..*(..))")
public void logAfterMethodExecution(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().toShortString();
log.debug("Completed execution of method: {}", methodName);
}
@Around("execution(* com.maoudia.app..*(..))")
public Object logMethodExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
String methodName = joinPoint.getSignature().toShortString();
log.debug("Method: {} executed in {} ms", methodName, executionTime);
return result;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maoudia.app</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maoudia-app</name>
<description>MAOUDIA APP</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment