ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

์šด์˜์„ ํ•˜๋‹ค๋ณด๋ฉด ์„œ๋น„์Šค๊ฐ€ ์‚ด์•„์žˆ๋Š”์ง€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์ด ์ž˜๋˜์—ˆ๋Š”์ง€ ์ฒดํฌ๋ฅผ ํ•˜๋Š”๋ฐ ํ•ด๋‹น ์ด๋ ฅ๋“ค์€ ๋กœ๊ทธ์—์„œ ์ œ์™ธํ•˜๊ณ  ์›ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋งŒ ๋กœ๊ทธ๋กœ ๋‚จ๊ธฐ๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

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 ์ปค์Šคํ…€ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๊ณ  ์‹ถ์€ ๋ฉ”์†Œ๋“œ์— ๋ถ™์—ฌ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.!!

๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ
๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (56)
๐Ÿ“บ Develop (34)
๐ŸŒ‹ Error Fixed (3)
๐Ÿ— Tool (3)
๐Ÿ’ป MacBook M1 (15)
๐Ÿ“ฆ ETC (1)

์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค.