From 26fec72122732c35de58e326affa0d4b62da616d Mon Sep 17 00:00:00 2001 From: woozu-shin Date: Tue, 10 Oct 2023 19:58:06 +0900 Subject: [PATCH] [NO-ISSUE] Add excludes filter --- ...uestResponseLoggingExampleApplication.java | 5 +++ .../logging/LoggingInterceptor.java | 17 ++++++-- .../logging/LoggingProperties.java | 41 +++++++++++++++++++ src/main/resources/application.yaml | 7 ++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingProperties.java diff --git a/src/main/java/com/myoa/engineering/sample/SpringBootRequestResponseLoggingExampleApplication.java b/src/main/java/com/myoa/engineering/sample/SpringBootRequestResponseLoggingExampleApplication.java index 6a97ba4..64531c3 100644 --- a/src/main/java/com/myoa/engineering/sample/SpringBootRequestResponseLoggingExampleApplication.java +++ b/src/main/java/com/myoa/engineering/sample/SpringBootRequestResponseLoggingExampleApplication.java @@ -1,8 +1,13 @@ package com.myoa.engineering.sample; +import com.myoa.engineering.sample.configuration.logging.LoggingProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +@EnableConfigurationProperties +@ConfigurationPropertiesScan(basePackageClasses= {LoggingProperties.class}) @SpringBootApplication public class SpringBootRequestResponseLoggingExampleApplication { diff --git a/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingInterceptor.java b/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingInterceptor.java index 7333452..d1e408c 100644 --- a/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingInterceptor.java +++ b/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingInterceptor.java @@ -21,9 +21,14 @@ import java.time.Instant; public class LoggingInterceptor implements HandlerInterceptor { private final LogPersistenceService logPersistenceService; + private final LoggingProperties.HttpMethod httpMethodProperties; + private final LoggingProperties.HttpStatus httpStatusProperties; - public LoggingInterceptor(LogPersistenceService logPersistenceService) { + public LoggingInterceptor(LogPersistenceService logPersistenceService, + LoggingProperties loggingProperties) { this.logPersistenceService = logPersistenceService; + this.httpMethodProperties = loggingProperties.getHttpMethod(); + this.httpStatusProperties = loggingProperties.getHttpStatus(); } @Override @@ -46,6 +51,9 @@ public class LoggingInterceptor implements HandlerInterceptor { } private boolean isLoggable(HttpServletRequest request) { + if (httpMethodProperties.isExcluded(request.getMethod())) { + return false; + } return request.getDispatcherType() != DispatcherType.ERROR; } @@ -76,7 +84,7 @@ public class LoggingInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { - if (isLoggable()) { + if (isLoggable(request.getMethod(), response.getStatus())) { ResponseLog responseLog = ResponseLog.builder() .httpStatus(response.getStatus()) .contentType(response.getContentType()) @@ -92,7 +100,10 @@ public class LoggingInterceptor implements HandlerInterceptor { HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } - private boolean isLoggable() { + private boolean isLoggable(String httpMethod, int httpStatus) { + if (httpMethodProperties.isExcluded(httpMethod) || httpStatusProperties.isExcluded(httpStatus)) { + return false; + } String traceId = MDC.get("traceId"); return traceId != null && traceId.isEmpty() == false; } diff --git a/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingProperties.java b/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingProperties.java new file mode 100644 index 0000000..b81b8e1 --- /dev/null +++ b/src/main/java/com/myoa/engineering/sample/configuration/logging/LoggingProperties.java @@ -0,0 +1,41 @@ +package com.myoa.engineering.sample.configuration.logging; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.Set; + +@Getter +@Setter +@Component +@ConfigurationProperties(prefix = "logging.web") +public class LoggingProperties { + + private HttpMethod httpMethod; + private HttpStatus httpStatus; + + @Getter + @Setter + @NoArgsConstructor + public static class HttpMethod { + Set excludes; + + public boolean isExcluded(String httpMethod) { + return excludes.contains(httpMethod); + } + } + + @Getter + @Setter + @NoArgsConstructor + public static class HttpStatus { + Set excludes; + + public boolean isExcluded(int httpStatus) { + return excludes.contains(httpStatus); + } + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 9efc7c7..33cd5cd 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -20,3 +20,10 @@ logging: level: org.hibernate.SQL: DEBUG org.hibernate.type.descriptor.sql: TRACE + web: + http-method: + excludes: + - GET + http-status: + excludes: + - 404