본문 바로가기
IT/Spring

2023-01-18-aop 사용한 로깅

by 봉즙 2023. 2. 28.

layout : post
title : "aop 사용한 로깅"

category : Spring

aop 를 활용한 로그를 남기려 한다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/basic.xml"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%green(%d{yyyy-MM-dd HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n
            </pattern>
        </layout>
    </appender>
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

point cut 으로 get post put delete 어노테이션이 있는 메서드들에 대해서 작동하도록 한다.

@Component
@Aspect
@Slf4j
@RequiredArgsConstructor
public class ApiLog {

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.PostMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.PutMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
    public void logInfo() {
    }

    @Around(value = "logInfo()")
    public Object aspectGet(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        return this.getObject(proceedingJoinPoint);
    }

    private Object getObject(final @NotNull ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        final HttpServletRequest request =
                ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
                        .getRequest();
        log.info("url : {}", request.getRequestURI());
    }
}

'IT > Spring' 카테고리의 다른 글

response null 인경우 생략  (0) 2023.02.28
bucket4j  (3) 2023.02.28
spring batch 5.0  (0) 2023.02.28
2022-01-04-Ip WhiteList  (0) 2023.02.28
2021-11-16-SpringAdmin  (0) 2023.02.28

댓글