ํฐ์คํ ๋ฆฌ ๋ทฐ
๐บ Develop/๐ด Spring
AOP๋ฅผ ์ฌ์ฉํ์ฌ custom Annotation์ Log๋ง ์ถ๋ ฅํ๊ธฐ
์ฃผ๋ฌ 2023. 7. 17. 17:41๋ฐ์ํ
โ AOP๋ฅผ ์ฌ์ฉํ์ฌ custom Annotation์ Log๋ง ์ถ๋ ฅํ๊ธฐ
์ด์์ ํ๋ค๋ณด๋ฉด ์๋น์ค๊ฐ ์ด์์๋์ง ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ด ์๋์๋์ง ์ฒดํฌ๋ฅผ ํ๋๋ฐ ํด๋น ์ด๋ ฅ๋ค์ ๋ก๊ทธ์์ ์ ์ธํ๊ณ ์ํ๋ ๋ฉ์๋๋ง ๋ก๊ทธ๋ก ๋จ๊ธฐ๊ณ ์ถ์๋ ์ฌ์ฉํ๋ฉด ๋๋ค.
Custom Annotaion์ ๋ง๋ค๊ธฐ
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoggerTarget {
}
LoggerTarget ์ด๋ผ๋ ์ด๋ฆ์ผ๋ก Annotation์ ๋ง๋ค์ด์คฌ๋ค. ๋ฉ์๋์์ญ์ด๊ณ ๋ฐํ์๋ฒ์๊น์ง ๋๋๋ก ๋ง๋ค์ด์ค๋ค.
โ AOP ์ค์ ํ๊ธฐ
@Aspect
@Component
@Slf4j
public class LoggingAspect {
private final String ANNOTATION_LOGGER_TARGET = "@annotation(com.sample.testapi.infrastructure.configuration.aop.LoggerTarget)";
@Before(ANNOTATION_LOGGER_TARGET)
public void loggingBefore(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
log.info("## REQUEST [{}] {} {}, Payload: {}", request.getRemoteAddr(), request.getMethod(), request.getRequestURI(), getPayload(joinPoint));
}
@AfterReturning(value = ANNOTATION_LOGGER_TARGET, returning = "result")
public void afterReturn(Object result) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
log.info("## RESPONSE [{}] {} {}, Result: {}", request.getRemoteAddr(), request.getMethod(), request.getRequestURI(), result.toString());
}
@AfterThrowing(value = ANNOTATION_LOGGER_TARGET, throwing = "e")
public void loggingAfterThrowing(Exception e) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
log.info("## RESPONSE [{}] {} {}, Exception: {}", request.getRemoteAddr(), request.getMethod(), request.getRequestURI(), e.toString());
}
private String getPayload(JoinPoint joinPoint) {
return Arrays.stream(joinPoint.getArgs())
.map(Object::toString)
.collect(Collectors.joining(", "));
}
}
AOP ์ค์ ์ ํ ์ดํ ๋ก๊ทธ๊ฐ ๋์ค๊ธฐ๋ฅผ ์ํ๋ ๋ฉ์๋์ @LoggerTarget ์ด๋ ธํ ์ด์ ์ ๋ถ์ฌ์ฃผ์
//Controller
@LoggerTarget
@ResponseStatus(HttpStatus.OK)
@PostMapping(value = "/schTest")
public ResponseEntity controllerLogTest(@RequestBody @Valid SaleProdRequest saleProdRequest) {
log.info("in");
return ResponseEntity.ok("test");
}
@LoggerTarget ์ปค์คํ ์ด๋ ธํ ์ด์ ์ผ๋ก ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ณ ์ถ์ ๋ฉ์๋์ ๋ถ์ฌ์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค.!!
'๐บ Develop > ๐ด Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Springboot console ๋ก๊ทธ๋ฅผ ์๋ฆ๋ต๊ฒ ๊พธ๋ฉฐ๋ณด์ (0) | 2024.05.11 |
---|---|
Spring MDC๋ฅผ ์ฌ์ฉํด ์๋ณ๊ฐ๋ฅํ ๋ก๊ทธ ๋จ๊ธฐ๊ธฐ (0) | 2023.07.17 |
Springboot graceful shutdown ์ ์ฉํ๊ธฐ (0) | 2023.07.14 |
Springboot ์ฝ์ ๋ฐฐ๋ ๋ณ๊ฒฝ (0) | 2023.03.05 |
[Springboot] Jasypt ์ค์น ๋ฐ ์ค์ (0) | 2022.10.11 |
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- ๋์์ธํจํด ์ฅ๋จ์
- ๋งฅ๋ฆฐ์ด ์ฑ ์ถ์ฒ
- ๋งฅ ์ฑ ์ถ์ฒ
- git user.gmail
- ๋์์ธํจํด ๋จ์
- ๊ฐ์ฒด ์งํฅ ์ค๊ณ ์์น
- git gmail
- git name
- ๊ฐ์ฒด์งํฅ์ค๊ณ solid
- ๋์์ธํจํด ์ฅ์
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
๊ธ ๋ณด๊ดํจ