[NO-ISSUE] Add excludes filter

This commit is contained in:
woozu-shin 2023-10-10 19:58:06 +09:00
parent f4ffd2ec4e
commit 26fec72122
4 changed files with 67 additions and 3 deletions

View File

@ -1,8 +1,13 @@
package com.myoa.engineering.sample; package com.myoa.engineering.sample;
import com.myoa.engineering.sample.configuration.logging.LoggingProperties;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 @SpringBootApplication
public class SpringBootRequestResponseLoggingExampleApplication { public class SpringBootRequestResponseLoggingExampleApplication {

View File

@ -21,9 +21,14 @@ import java.time.Instant;
public class LoggingInterceptor implements HandlerInterceptor { public class LoggingInterceptor implements HandlerInterceptor {
private final LogPersistenceService logPersistenceService; 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.logPersistenceService = logPersistenceService;
this.httpMethodProperties = loggingProperties.getHttpMethod();
this.httpStatusProperties = loggingProperties.getHttpStatus();
} }
@Override @Override
@ -46,6 +51,9 @@ public class LoggingInterceptor implements HandlerInterceptor {
} }
private boolean isLoggable(HttpServletRequest request) { private boolean isLoggable(HttpServletRequest request) {
if (httpMethodProperties.isExcluded(request.getMethod())) {
return false;
}
return request.getDispatcherType() != DispatcherType.ERROR; return request.getDispatcherType() != DispatcherType.ERROR;
} }
@ -76,7 +84,7 @@ public class LoggingInterceptor implements HandlerInterceptor {
@Override @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 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() ResponseLog responseLog = ResponseLog.builder()
.httpStatus(response.getStatus()) .httpStatus(response.getStatus())
.contentType(response.getContentType()) .contentType(response.getContentType())
@ -92,7 +100,10 @@ public class LoggingInterceptor implements HandlerInterceptor {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex); 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"); String traceId = MDC.get("traceId");
return traceId != null && traceId.isEmpty() == false; return traceId != null && traceId.isEmpty() == false;
} }

View File

@ -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<String> excludes;
public boolean isExcluded(String httpMethod) {
return excludes.contains(httpMethod);
}
}
@Getter
@Setter
@NoArgsConstructor
public static class HttpStatus {
Set<Integer> excludes;
public boolean isExcluded(int httpStatus) {
return excludes.contains(httpStatus);
}
}
}

View File

@ -20,3 +20,10 @@ logging:
level: level:
org.hibernate.SQL: DEBUG org.hibernate.SQL: DEBUG
org.hibernate.type.descriptor.sql: TRACE org.hibernate.type.descriptor.sql: TRACE
web:
http-method:
excludes:
- GET
http-status:
excludes:
- 404