Skip to content

Commit 013540a

Browse files
authored
Allow configuring log level (#26)
1 parent 52e77e0 commit 013540a

File tree

9 files changed

+31
-8
lines changed

9 files changed

+31
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ openapi.validation.specification-file-path=/tmp/openapi-spec/openapi.json
8888
# If it is within src/main/resources/folder/my-spec.json use
8989
openapi.validation.specification-file-path=folder/my-spec.json
9090

91+
# Custom log level for violations (ignore, info, warn, error)
92+
# Default: info
93+
openapi.validation.violation-log-level=error
94+
9195
# Comma separated list of paths to be excluded from validation. Default is no excluded paths
9296
openapi.validation.excluded-paths=/_readiness,/_liveness,/_metrics
9397
# Allows to exclude requests based on headers. Default is no excluded headers.

examples/example-spring-boot-starter-web/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
openapi.validation.sample-rate=1
22
openapi.validation.specification-file-path=openapi.yaml
3+
openapi.validation.violation-log-level=
34
openapi.validation.excluded-paths=/_readiness,/_liveness,/_metrics
45
openapi.validation.validation-report-throttle-wait-seconds=10
56
openapi.validation.validation-report-metric-name=openapi_error

openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/metrics/DefaultMetricsReporter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.getyourguide.openapi.validation.api.metrics;
22

3+
import com.getyourguide.openapi.validation.api.log.LogLevel;
34
import com.getyourguide.openapi.validation.api.metrics.client.MetricsClient;
45
import com.getyourguide.openapi.validation.api.model.OpenApiViolation;
56
import java.util.ArrayList;
@@ -16,6 +17,10 @@ public class DefaultMetricsReporter implements MetricsReporter {
1617

1718
@Override
1819
public void reportViolation(OpenApiViolation violation) {
20+
if (violation.getLevel() == LogLevel.IGNORE) {
21+
return;
22+
}
23+
1924
metricsClient.increment(buildMetricName(".error"), createTagsForViolation(violation));
2025
}
2126

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/DefaultViolationLogger.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public void log(OpenApiViolation violation) {
2222
case INFO -> log.info(violation.getLogMessage());
2323
case WARN -> log.warn(violation.getLogMessage());
2424
case ERROR -> log.error(violation.getLogMessage());
25-
// keeping ignored as debug for now
26-
case IGNORE -> log.debug(violation.getLogMessage());
2725
default -> { /* do nothing */ }
2826
}
2927
} catch (IOException e) {

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiInteractionValidatorFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ private LevelResolver buildLevelResolver(
145145
Map<String, LogLevel> levelResolverLevels,
146146
LogLevel levelResolverDefaultLevel
147147
) {
148-
var builder = LevelResolver.create();
148+
var builder = LevelResolver.create()
149+
// This is needed if your spec uses composition via allOf, anyOf or oneOf.
150+
.withLevel("validation.schema.additionalProperties", ValidationReport.Level.IGNORE);
149151
if (levelResolverLevels != null && !levelResolverLevels.isEmpty()) {
150152
builder.withLevels(
151153
levelResolverLevels.entrySet().stream()
@@ -154,10 +156,10 @@ private LevelResolver buildLevelResolver(
154156
)
155157
);
156158
}
157-
return builder
158-
// this will cause all messages to be warn by default
159-
.withDefaultLevel(mapLevel(levelResolverDefaultLevel).orElse(ValidationReport.Level.INFO))
160-
.build();
159+
160+
mapLevel(levelResolverDefaultLevel).ifPresent(builder::withDefaultLevel);
161+
162+
return builder.build();
161163
}
162164

163165
private Optional<ValidationReport.Level> mapLevel(LogLevel level) {

spring-boot-starter/spring-boot-starter-core/src/main/java/com/getyourguide/openapi/validation/OpenApiValidationApplicationProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.getyourguide.openapi.validation.OpenApiValidationApplicationProperties.PROPERTY_PREFIX;
44

55
import com.getyourguide.openapi.validation.api.exclusions.ExcludedHeader;
6+
import com.getyourguide.openapi.validation.api.log.LogLevel;
67
import com.getyourguide.openapi.validation.api.metrics.MetricTag;
78
import com.getyourguide.openapi.validation.util.CommaSeparatedStringsUtil;
89
import java.util.Arrays;
@@ -27,6 +28,7 @@ public class OpenApiValidationApplicationProperties {
2728

2829
private Double sampleRate;
2930
private String specificationFilePath;
31+
private LogLevel violationLogLevel;
3032
private Integer validationReportThrottleWaitSeconds;
3133
private String validationReportMetricName;
3234
private String validationReportMetricAdditionalTags;

spring-boot-starter/spring-boot-starter-core/src/main/java/com/getyourguide/openapi/validation/autoconfigure/LibraryAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public ValidationReportHandler validationReportHandler(
9191
public ValidatorConfiguration validatorConfiguration() {
9292
return new ValidatorConfigurationBuilder()
9393
// .levelResolverLevel("validation.request.body.schema.additionalProperties", LogLevel.IGNORE)
94-
.levelResolverDefaultLevel(LogLevel.INFO)
94+
.levelResolverDefaultLevel(
95+
properties.getViolationLogLevel() != null ? properties.getViolationLogLevel() : LogLevel.INFO
96+
)
9597
.build();
9698
}
9799

spring-boot-starter/spring-boot-starter-core/src/main/resources/META-INF/spring-configuration-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"type": "java.lang.String",
1111
"description": "Path to the OpenAPI specification file. If set, the system will search for this file within resources and also as a path based on the current (working) directory. The property defaults to null. If null, the program searches for either an 'openapi.yaml/json' or a 'spec.yaml/json' file under resources."
1212
},
13+
{
14+
"name": "openapi.validation.violation-log-level",
15+
"type": "com.getyourguide.openapi.validation.api.log.LogLevel",
16+
"description": "Log level to be used for all violations. Default: info\nNote: This has no effect if a custom ValidatorConfiguration is provided, then it needs to be configured there."
17+
},
1318
{
1419
"name": "openapi.validation.excluded-paths",
1520
"type": "java.lang.String",

spring-boot-starter/spring-boot-starter-core/src/test/java/com/getyourguide/openapi/validation/OpenApiValidationApplicationPropertiesTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import com.getyourguide.openapi.validation.api.exclusions.ExcludedHeader;
8+
import com.getyourguide.openapi.validation.api.log.LogLevel;
89
import com.getyourguide.openapi.validation.api.metrics.MetricTag;
910
import java.util.List;
1011
import java.util.Set;
@@ -14,6 +15,7 @@ class OpenApiValidationApplicationPropertiesTest {
1415

1516
private static final Double SAMPLE_RATE = 0.001;
1617
private static final String SPECIFICATION_FILE_PATH = "/tmp/openapi.yaml";
18+
private static final LogLevel LOG_LEVEL = LogLevel.ERROR;
1719
private static final Integer VALIDATION_REPORT_THROTTLE_WAIT_SECONDS = 10;
1820
private static final String VALIDATION_REPORT_METRIC_NAME = "openapi_validation";
1921
private static final String VALIDATION_REPORT_METRIC_ADDITONAL_TAGS_STRING = "service=payment,team=chk";
@@ -25,6 +27,7 @@ void getters() {
2527
var loggingConfiguration = new OpenApiValidationApplicationProperties(
2628
SAMPLE_RATE,
2729
SPECIFICATION_FILE_PATH,
30+
LOG_LEVEL,
2831
VALIDATION_REPORT_THROTTLE_WAIT_SECONDS,
2932
VALIDATION_REPORT_METRIC_NAME,
3033
VALIDATION_REPORT_METRIC_ADDITONAL_TAGS_STRING,
@@ -36,6 +39,7 @@ void getters() {
3639

3740
assertEquals(SAMPLE_RATE, loggingConfiguration.getSampleRate());
3841
assertEquals(SPECIFICATION_FILE_PATH, loggingConfiguration.getSpecificationFilePath());
42+
assertEquals(LOG_LEVEL, loggingConfiguration.getViolationLogLevel());
3943
assertEquals(VALIDATION_REPORT_THROTTLE_WAIT_SECONDS,
4044
loggingConfiguration.getValidationReportThrottleWaitSeconds());
4145
assertEquals(VALIDATION_REPORT_METRIC_NAME, loggingConfiguration.getValidationReportMetricName());

0 commit comments

Comments
 (0)