Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] 로깅 모듈이 인터셉터를 사용하여 동작하도록 수정 #323

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
package org.kakaoshare.backend.logging.config;

import lombok.RequiredArgsConstructor;
import org.kakaoshare.backend.logging.interceptor.LoggingInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StopWatch;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class LoggingConfig {
@RequiredArgsConstructor
public class LoggingConfig implements WebMvcConfigurer {
private static final String METRIC_URL_PREFIX = "/actuator";
private static final String FAVICON_URL = "/favicon.ico";

private final LoggingInterceptor loggingInterceptor;

@Bean
@RequestScope
public StopWatch stopWatch() {
return new StopWatch();
}

@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(loggingInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(METRIC_URL_PREFIX, FAVICON_URL);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package org.kakaoshare.backend.logging.filter;
package org.kakaoshare.backend.logging.interceptor;


import com.querydsl.core.util.StringUtils;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.kakaoshare.backend.logging.util.ApiQueryCounter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
Expand All @@ -26,8 +25,7 @@

@Component
@Slf4j
@RequiredArgsConstructor
public class LoggingFilter extends OncePerRequestFilter {
public class LoggingInterceptor extends WebRequestHandlerInterceptorAdapter {
private static final String REQUEST_LOG_NO_BODY_FORMAT = "REQUEST :: METHOD: {}, URL: {}, HAS_AUTHORIZATION: {}";
private static final String REQUEST_LOG_FORMAT = REQUEST_LOG_NO_BODY_FORMAT + ", BODY: {}";
private static final String RESPONSE_LOG_NO_BODY_FORMAT = "RESPONSE :: STATUS_CODE: {}, METHOD: {}, URL: {}, QUERY_COUNT: {}, TIME_TAKEN: {}ms";
Expand All @@ -38,27 +36,32 @@ public class LoggingFilter extends OncePerRequestFilter {
private static final String PARAM_DELIMITER = "&";
private static final String KEY_VALUE_DELIMITER = "=";

private static final String METRIC_URL_PREFIX = "/actuator";
private static final String FAVICON_URL = "/favicon.ico";

private final StopWatch apiTimer;
private final ApiQueryCounter apiQueryCounter;

@Override
protected void doFilterInternal(final HttpServletRequest request,
final HttpServletResponse response,
final FilterChain filterChain) throws ServletException, IOException {
final ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
final ContentCachingResponseWrapper cachingResponse = new ContentCachingResponseWrapper(response);
final String requestURI = cachingRequest.getRequestURI();
if (requestURI.contains(METRIC_URL_PREFIX) || requestURI.contains(FAVICON_URL)) {
return;
}
@Autowired
public LoggingInterceptor(final WebRequestInterceptor requestInterceptor,
final StopWatch apiTimer,
final ApiQueryCounter apiQueryCounter) {
super(requestInterceptor);
this.apiTimer = apiTimer;
this.apiQueryCounter = apiQueryCounter;
}

@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
apiTimer.start();
filterChain.doFilter(cachingRequest, cachingResponse);
apiTimer.stop();
return true;
}

@Override
public void afterCompletion(final HttpServletRequest request,
final HttpServletResponse response,
final Object handler,
final Exception ex) throws Exception {
apiTimer.stop();
final ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
final ContentCachingResponseWrapper cachingResponse = new ContentCachingResponseWrapper(response);
logRequestAndResponse(cachingRequest, cachingResponse);
cachingResponse.copyBodyToResponse();
}
Expand Down
Loading