AOP helps add logging to your app without changing your main code. It keeps your code clean and easy to read.
0
0
AOP for logging in Spring Boot
Introduction
You want to track when methods start and finish in your app.
You need to log errors automatically without adding code everywhere.
You want to measure how long certain actions take.
You want to keep your business logic separate from logging details.
Syntax
Spring Boot
@Aspect @Component public class LoggingAspect { @Before("execution(* com.example..*(..))") public void logBefore(JoinPoint joinPoint) { // logging code here } @AfterReturning(pointcut = "execution(* com.example..*(..))", returning = "result") public void logAfter(JoinPoint joinPoint, Object result) { // logging code here } }
@Aspect marks the class as an aspect for AOP.
@Before runs code before matched methods; @AfterReturning runs after methods return.
Examples
This logs the start of any method in the service package.
Spring Boot
@Before("execution(* com.example.service.*.*(..))") public void logBeforeMethod(JoinPoint joinPoint) { System.out.println("Starting: " + joinPoint.getSignature().getName()); }
This logs the end of controller methods and shows what they returned.
Spring Boot
@AfterReturning(pointcut = "execution(* com.example.controller.*.*(..))", returning = "result") public void logAfterMethod(JoinPoint joinPoint, Object result) { System.out.println("Finished: " + joinPoint.getSignature().getName() + ", returned: " + result); }
Sample Program
This Spring Boot app uses AOP to log before and after the add method in CalculatorService. The logs show method start, finish, and return value.
Spring Boot
package com.example.aoplogging; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.aoplogging.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("[LOG] Starting method: " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "execution(* com.example.aoplogging.service.*.*(..))", returning = "result") public void logAfter(JoinPoint joinPoint, Object result) { System.out.println("[LOG] Finished method: " + joinPoint.getSignature().getName() + ", returned: " + result); } } package com.example.aoplogging.service; import org.springframework.stereotype.Service; @Service public class CalculatorService { public int add(int a, int b) { return a + b; } } package com.example.aoplogging; import com.example.aoplogging.service.CalculatorService; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class AopLoggingApplication { public static void main(String[] args) { SpringApplication.run(AopLoggingApplication.class, args); } @Bean public CommandLineRunner run(CalculatorService calculatorService) { return args -> { int result = calculatorService.add(5, 3); System.out.println("Result: " + result); }; } }
OutputSuccess
Important Notes
Make sure to add @EnableAspectJAutoProxy in your Spring Boot main class or config.
Use pointcut expressions carefully to target only the methods you want to log.
Logging too much can slow your app, so log only important methods.
Summary
AOP lets you add logging without changing your main code.
Use @Before and @AfterReturning to log method start and end.
Keep your code clean and separate from logging details.