Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Logging With AOP in Spring

$
0
0

1. Overview

We often use logging to document meaningful steps and valuable information during the program execution. It allows us to record data we can use later to debug and analyze the code.

Additionally, Aspect-Oriented Programming (or AOP for short) is a paradigm that lets us segregate cross-cutting concerns, such as transaction management or logging, throughout the application without cluttering the business logic.

In this tutorial, we’ll learn how to implement logging using the AOP and Spring framework.

2. Logging Without AOP

When it comes to logging, we usually put logs at the beginning and the end of the methods. This way, we can easily track the application execution flow. In addition, we can capture the values being passed to specific methods and the values they return.

To demonstrate, let’s create the GreetingService class with the greet() method:

public String greet(String name) {
    logger.info(">> greet() - {}", name);
    String result = String.format("Hello %s", name);
    logger.info("<< greet() - {}", result);
    return result;
}

Even though the implementation above seems like a standard solution, logging statements can feel like unnecessary clutter in our code.

Furthermore, we introduced additional complexity to our code. Without logging, we could rewrite this method as a one-liner:

public String greet(String name) {
    return String.format("Hello %s", name);
}

3. Aspect-Oriented Programming

As the name suggests, Aspect-Oriented Programming focuses on aspects rather than objects and classes. We use AOP to implement additional functionality for specific application parts without modifying their current implementations.

3.1. AOP Concepts

Before we dive in, let’s examine the basic AOP concepts at a very high level.

  • Aspect: The cross-cutting concern or the functionality we’d like to apply throughout the application.
  • Join Point: The point of the application flow where we want to apply an aspect.
  • Advice: The action that should be executed at a specific join point.
  • Pointcut: Collection of join points where an aspect should be applied.

Furthermore, it’s worth noting that Spring AOP only supports join points for method execution. We should consider using compile-time libraries such as AspectJ to create aspects for fields, constructors, static initializers, etc.

3.2. Maven Dependency

To use Spring AOP, let’s add the spring-boot-starter-aop dependency in our pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

4. Logging With AOP

One way to implement AOP in Spring is by using a Spring bean annotated with the @Aspect annotation:

@Aspect
@Component
public class LoggingAspect {
}

The @Aspect annotation serves as a marker annotation, so Spring won’t automatically treat it as a component. To indicate it should be a bean managed by Spring and detected through component scanning, we also annotate the class with the @Component annotation.

Next, let’s define a pointcut. Simply put, pointcuts allow us to specify which join point execution we want to intercept with an aspect:

@Pointcut("execution(public * com.baeldung.logging.*.*(..))")
private void publicMethodsFromLoggingPackage() {
}

Here, we defined a pointcut expression that includes only public methods from the com.baeldung.logging package.

Moving forward, let’s see how to define the advice to log the start and the end of the method execution.

4.1. Using Around Advice

We’ll start with the more general advice type – the Around advice. It allows us to implement custom behavior before and after the method invocation. Moreover, with this advice, we can decide whether to proceed with the specific join point, return a custom result, or throw an exception.

Let’s define the advice using the @Around annotation:

@Around(value = "publicMethodsFromLoggingPackage()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    String methodName = joinPoint.getSignature().getName();
    logger.info(">> {}() - {}", methodName, Arrays.toString(args));
    Object result = joinPoint.proceed();
    logger.info("<< {}() - {}", methodName, result);
    return result;
}

The value attribute associates this Around advice with the previously defined pointcut. The advice runs around method executions matched by the publicMethodsFromLoggingPackage() pointcut signature.

The method accepts a ProceedingJoinPoint parameter. It’s a subclass of the JoinPoint class, allowing us to call the proceed() method to execute the next advice (if it exists) or the target method.

We call the getArgs() method on the joinPoint to retrieve the array of method arguments. Additionally, we use the getSignature().getName() method to get the name of the method we’re intercepting.

Next, we call the proceed() method to execute the target method and retrieve the result.

Finally, let’s call the greet() method we mentioned earlier:

@Test
void givenName_whenGreet_thenReturnCorrectResult() {
    String result = greetingService.greet("Baeldung");
    assertNotNull(result);
    assertEquals("Hello Baeldung", result);
}

After running our test, we can see the following result in our console:

>> greet() - [Baeldung]
<< greet() - Hello Baeldung

5. Using the Least Invasive Advice

When deciding which type of advice to use, it’s recommended that we use the least powerful advice that serves our needs. If we choose a general advice, such as Around advice, we’re more prone to potential errors and performance issues.

That’s to say, let’s examine how to accomplish the same functionality, but this time using the Before and After advices. Unlike Around advice, they don’t wrap the method execution, thus, there’s no need to explicitly call the proceed() method to continue with join point execution. Specifically, we use these types of advice to intercept methods right before or after execution.

5.1. Using Before Advice

To intercept the method before its execution, we’ll create an advice using the @Before annotation:

@Before(value = "publicMethodsFromLoggingPackage()")
public void logBefore(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    String methodName = joinPoint.getSignature().getName();
    logger.info(">> {}() - {}", methodName, Arrays.toString(args));
}

Similar to the previous example, we used the getArgs() method to get method arguments and the getSignature().getName() method to get the method name.

5.2. Using AfterReturning Advice

Going further, to add a log after the method execution, we’ll create the @AfterReturning advice that runs if a method execution completes without throwing any exception:

@AfterReturning(value = "publicMethodsFromLoggingPackage()", returning = "result")
public void logAfter(JoinPoint joinPoint, Object result) {
    String methodName = joinPoint.getSignature().getName();
    logger.info("<< {}() - {}", methodName, result);
}

Here, we defined the returning attribute to get the value returned from the method. Additionally, the value we provided in the attribute should match the parameter’s name. The return value will be passed to the advice method as an argument.

5.3. Using AfterThrowing Advice

On the other hand, to log situations when the method invocation completes with an exception, we could use the @AfterThrowing advice:

@AfterThrowing(pointcut = "publicMethodsFromLoggingPackage()", throwing = "exception")
public void logException(JoinPoint joinPoint, Throwable exception) {
    String methodName = joinPoint.getSignature().getName();
    logger.error("<< {}() - {}", methodName, exception.getMessage());
}

This time, instead of the return value, we’ll get the thrown exception in our advice method.

6. Spring AOP Pitfalls

Lastly, let’s discuss some concerns we should consider when working with Spring AOP.

Spring AOP is a proxy-based framework. It creates proxy objects to intercept method calls and apply logic defined in advice. This can negatively impact the performance of our application.

To reduce the effect of AOP on performance, we should consider using AOP only when necessary. We should avoid creating aspects for isolated and infrequent operations.

Finally, if we use AOP for development purposes only, we can create it conditionally, for instance, only if a specific Spring profile is active.

7. Conclusion

In this article, we learned how to perform logging using Spring AOP.

To sum up, we examined how to implement logging using Around advice as well as Before and After advice. We also explored why it’s important to use the least powerful advice to fit our needs. Finally, we addressed some potential issues Spring AOP brings to the table.

As always, the entire code examples can be found over on GitHub.

       

Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>