BAEL-7413 - Logging With AOP in Spring (#15810)
* BAEL-7413 - Logging With AOP in Spring * Add GreetingServiceWithoutAOP class
This commit is contained in:
parent
cf5842a3ab
commit
f763404388
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.logging;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class GreetingService {
|
||||
|
||||
public String greet(String name) {
|
||||
return String.format("Hello %s", name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.logging;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class GreetingServiceWithoutAOP {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GreetingServiceWithoutAOP.class);
|
||||
|
||||
public String greet(String name) {
|
||||
logger.info(">> greet() - {}", name);
|
||||
String result = String.format("Hello %s", name);
|
||||
logger.info("<< greet() - {}", result);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.logging;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
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.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class LoggingAspect {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
|
||||
|
||||
@Pointcut("execution(public * com.baeldung.logging.*.*(..))")
|
||||
private void publicMethodsFromLoggingPackage() {
|
||||
}
|
||||
|
||||
@Before(value = "publicMethodsFromLoggingPackage()")
|
||||
public void logBefore(JoinPoint joinPoint) {
|
||||
Object[] args = joinPoint.getArgs();
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
logger.info(">> {}() - {}", methodName, Arrays.toString(args));
|
||||
}
|
||||
|
||||
@AfterReturning(value = "publicMethodsFromLoggingPackage()", returning = "result")
|
||||
public void logAfter(JoinPoint joinPoint, Object result) {
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
logger.info("<< {}() - {}", methodName, result);
|
||||
}
|
||||
|
||||
@AfterThrowing(pointcut = "publicMethodsFromLoggingPackage()", throwing = "exception")
|
||||
public void logException(JoinPoint joinPoint, Throwable exception) {
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
logger.error("<< {}() - {}", methodName, exception.getMessage());
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.logging;
|
||||
|
||||
import com.baeldung.Application;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@SpringBootTest(classes = Application.class)
|
||||
class GreetingServiceUnitTest {
|
||||
|
||||
@Autowired
|
||||
private GreetingService greetingService;
|
||||
|
||||
@Test
|
||||
void givenName_whenGreet_thenReturnCorrectResult() {
|
||||
String result = greetingService.greet("Baeldung");
|
||||
assertNotNull(result);
|
||||
assertEquals("Hello Baeldung", result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue