Skip to content

Commit 028710e

Browse files
authored
Optimize logging by creating a debug flag (#1054)
* Optimize debug to reduce array allocation due to var-args * Optimize logging by creating a debug flag
1 parent 2d645e6 commit 028710e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+95
-53
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ The following is sample output from the Hierarchical format.
510510
| `locale` | The locale to use for generating messages in the `ValidationMessage`. Note that this value is copied from `SchemaValidatorsConfig` for each execution. | `Locale.getDefault()`
511511
| `failFast` | Whether to return failure immediately when an assertion is generated. Note that this value is copied from `SchemaValidatorsConfig` for each execution but is automatically set to `true` for the Boolean and Flag output formats. | `false`
512512
| `formatAssertionsEnabled` | The default is to generate format assertions from Draft 4 to Draft 7 and to only generate annotations from Draft 2019-09. Setting to `true` or `false` will override the default behavior. | `null`
513+
| `debugEnabled` | Controls whether debug logging is enabled for logging the node information when processing. Note that this will generate a lot of logs that will affect performance. | `false`
513514

514515
### Schema Validators Configuration
515516

src/main/java/com/networknt/schema/AdditionalPropertiesValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
8888

8989
protected Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
9090
JsonNodePath instanceLocation, boolean walk) {
91-
debug(logger, node, rootNode, instanceLocation);
91+
debug(logger, executionContext, node, rootNode, instanceLocation);
9292
if (!node.isObject()) {
9393
// ignore no object
9494
return Collections.emptySet();

src/main/java/com/networknt/schema/AllOfValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
5656
}
5757

5858
protected Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean walk) {
59-
debug(logger, node, rootNode, instanceLocation);
59+
debug(logger, executionContext, node, rootNode, instanceLocation);
6060

6161
SetView<ValidationMessage> childSchemaErrors = null;
6262

src/main/java/com/networknt/schema/AnyOfValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
6060

6161
protected Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
6262
JsonNodePath instanceLocation, boolean walk) {
63-
debug(logger, node, rootNode, instanceLocation);
63+
debug(logger, executionContext, node, rootNode, instanceLocation);
6464

6565
if (this.validationContext.getConfig().isOpenAPI3StyleDiscriminators()) {
6666
executionContext.enterDiscriminatorContext(new DiscriminatorContext(), instanceLocation);

src/main/java/com/networknt/schema/BaseJsonValidator.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,23 @@ protected static boolean equals(double n1, double n2) {
9696
return Math.abs(n1 - n2) < 1e-12;
9797
}
9898

99-
protected static void debug(Logger logger, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
100-
logger.debug("validate( {}, {}, {})", node, rootNode, instanceLocation);
99+
public static void debug(Logger logger, ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
100+
JsonNodePath instanceLocation) {
101+
//logger.debug("validate( {}, {}, {})", node, rootNode, instanceLocation);
102+
// The below is equivalent to the above but as there are more than 2 arguments
103+
// the var-arg method is used and an array needs to be allocated even if debug
104+
// is not enabled
105+
if (executionContext.getExecutionConfig().isDebugEnabled() && logger.isDebugEnabled()) {
106+
StringBuilder builder = new StringBuilder();
107+
builder.append("validate( ");
108+
builder.append(node.toString());
109+
builder.append(", ");
110+
builder.append(rootNode.toString());
111+
builder.append(", ");
112+
builder.append(instanceLocation.toString());
113+
builder.append(")");
114+
logger.debug(builder.toString());
115+
}
101116
}
102117

103118
/**

src/main/java/com/networknt/schema/ConstValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public ConstValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
3434
}
3535

3636
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
37-
debug(logger, node, rootNode, instanceLocation);
37+
debug(logger, executionContext, node, rootNode, instanceLocation);
3838

3939
if (schemaNode.isNumber() && node.isNumber()) {
4040
if (schemaNode.decimalValue().compareTo(node.decimalValue()) != 0) {

src/main/java/com/networknt/schema/ContainsValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ContainsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
7272

7373
@Override
7474
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
75-
debug(logger, node, rootNode, instanceLocation);
75+
debug(logger, executionContext, node, rootNode, instanceLocation);
7676

7777
// ignores non-arrays
7878
Set<ValidationMessage> results = null;

src/main/java/com/networknt/schema/ContentEncodingValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private boolean matches(String value) {
6666
@Override
6767
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
6868
JsonNodePath instanceLocation) {
69-
debug(logger, node, rootNode, instanceLocation);
69+
debug(logger, executionContext, node, rootNode, instanceLocation);
7070

7171
// Ignore non-strings
7272
JsonType nodeType = TypeFactory.getValueNodeType(node, this.validationContext.getConfig());

src/main/java/com/networknt/schema/ContentMediaTypeValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ else if (!PATTERN.matcher(this.contentMediaType).matches()) {
9090
@Override
9191
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
9292
JsonNodePath instanceLocation) {
93-
debug(logger, node, rootNode, instanceLocation);
93+
debug(logger, executionContext, node, rootNode, instanceLocation);
9494

9595
// Ignore non-strings
9696
JsonType nodeType = TypeFactory.getValueNodeType(node, this.validationContext.getConfig());

src/main/java/com/networknt/schema/DependenciesValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public DependenciesValidator(SchemaLocation schemaLocation, JsonNodePath evaluat
6363
}
6464

6565
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
66-
debug(logger, node, rootNode, instanceLocation);
66+
debug(logger, executionContext, node, rootNode, instanceLocation);
6767

6868
Set<ValidationMessage> errors = null;
6969

0 commit comments

Comments
 (0)