Skip to content

Commit

Permalink
Allow for custom logging levels when returning a default or optional …
Browse files Browse the repository at this point in the history
…value. It was too spammy for some.
  • Loading branch information
credmond-git committed Apr 28, 2023
1 parent 6447bab commit 905cc98
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {

allprojects {
group = "com.github.gestalt-config"
version = "0.20.5"
version = "0.20.6"
}


Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.WARNING;
import static java.lang.System.Logger.Level.INFO;

import static org.github.gestalt.config.entity.ValidationLevel.ERROR;
import static org.github.gestalt.config.entity.ValidationLevel.MISSING_VALUE;
Expand Down Expand Up @@ -333,30 +332,30 @@ private <T> T getConfigInternal(String path, boolean failOnErrors, T defaultVal,
throw new GestaltException("Failed getting config path: " + combinedPath + ", for class: " + klass.getName(),
results.getErrors());
} else {
if (logger.isLoggable(WARNING)) {
String errorMsg = ErrorsUtil.buildErrorMessage("Failed getting Optional config path: " + combinedPath +
if (logger.isLoggable(gestaltConfig.getLogLevelForMissingValuesWhenDefaultOrOptional())) {
String errorMsg = ErrorsUtil.buildErrorMessage("Failed getting config path: " + combinedPath +
", for class: " + klass.getName() + " returning empty Optional", results.getErrors());
logger.log(WARNING, errorMsg);
logger.log(DEBUG, errorMsg);
}

return defaultVal;
}

} else if (results.hasErrors() && logger.isLoggable(INFO)) {
String errorMsg = ErrorsUtil.buildErrorMessage("Errors getting Optional config path: " + combinedPath +
} else if (results.hasErrors() && logger.isLoggable(DEBUG)) {
String errorMsg = ErrorsUtil.buildErrorMessage("Errors getting config path: " + combinedPath +
", for class: " + klass.getName(), results.getErrors());
logger.log(INFO, errorMsg);
logger.log(DEBUG, errorMsg);
}

if (results.hasResults()) {
return results.results();
}
}

if (logger.isLoggable(INFO)) {
if (logger.isLoggable(gestaltConfig.getLogLevelForMissingValuesWhenDefaultOrOptional())) {
String errorMsg = ErrorsUtil.buildErrorMessage("No results for Optional config path: " + combinedPath +
", and class: " + klass.getName() + " returning empty Optional", tokens.getErrors());
logger.log(INFO, errorMsg);
logger.log(DEBUG, errorMsg);
}

if (failOnErrors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class GestaltBuilder {
private Boolean treatMissingValuesAsErrors = null;
private Boolean treatNullValuesInClassAsErrors = null;

private System.Logger.Level logLevelForMissingValuesWhenDefaultOrOptional = null;

private DateTimeFormatter dateDecoderFormat = null;
private DateTimeFormatter localDateTimeFormat = null;
private DateTimeFormatter localDateFormat = null;
Expand Down Expand Up @@ -553,6 +555,26 @@ public GestaltBuilder setDateDecoderFormat(DateTimeFormatter dateDecoderFormat)
return this;
}

/**
* Provide the log level when we log a message when a config is missing, but we provided a default, or it is Optional.
*
* @return Log level
*/
public System.Logger.Level getLogLevelForMissingValuesWhenDefaultOrOptional() {
return logLevelForMissingValuesWhenDefaultOrOptional;
}

/**
* Provide the log level when we log a message when a config is missing, but we provided a default, or it is Optional.
*
* @param logLevelForMissingValuesWhenDefaultOrOptional log level
* @return GestaltBuilder builder
*/
public GestaltBuilder setLogLevelForMissingValuesWhenDefaultOrOptional(System.Logger.Level logLevelForMissingValuesWhenDefaultOrOptional) {
this.logLevelForMissingValuesWhenDefaultOrOptional = logLevelForMissingValuesWhenDefaultOrOptional;
return this;
}

/**
* Set a local date time format. Used to decode local date times.
*
Expand Down Expand Up @@ -612,9 +634,11 @@ public Integer getMaxSubstitutionNestedDepth() {
* If you have nested or recursive substitutions that go deeper than this it will fail.
*
* @param maxSubstitutionNestedDepth the maximum string substitution nested depth.
* @return GestaltBuilder builder
*/
public void setMaxSubstitutionNestedDepth(Integer maxSubstitutionNestedDepth) {
public GestaltBuilder setMaxSubstitutionNestedDepth(Integer maxSubstitutionNestedDepth) {
this.maxSubstitutionNestedDepth = maxSubstitutionNestedDepth;
return this;
}

/**
Expand All @@ -632,9 +656,11 @@ public String getSubstitutionRegex() {
* Must have a named capture group transform, key, and default, where the key is required and the transform and default are optional.
*
* @param substitutionRegex the string substitution regex
* @return GestaltBuilder builder
*/
public void setSubstitutionRegex(String substitutionRegex) {
public GestaltBuilder setSubstitutionRegex(String substitutionRegex) {
this.substitutionRegex = substitutionRegex;
return this;
}


Expand Down Expand Up @@ -773,6 +799,9 @@ private GestaltConfig rebuildConfig() {
newConfig.setTreatNullValuesInClassAsErrors(Objects.requireNonNullElseGet(treatNullValuesInClassAsErrors,
() -> gestaltConfig.isTreatNullValuesInClassAsErrors()));

newConfig.setLogLevelForMissingValuesWhenDefaultOrOptional(Objects.requireNonNullElseGet(logLevelForMissingValuesWhenDefaultOrOptional,
() -> gestaltConfig.getLogLevelForMissingValuesWhenDefaultOrOptional()));

newConfig.setDateDecoderFormat(Objects.requireNonNullElseGet(dateDecoderFormat,
() -> gestaltConfig.getDateDecoderFormat()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class GestaltConfig {
private boolean treatMissingValuesAsErrors = false;
//Treat null values in classes after decoding as errors.
private boolean treatNullValuesInClassAsErrors = true;
//Provide the log level when we log a message when a config is missing, but we provided a default, or it is Optional.
private System.Logger.Level logLevelForMissingValuesWhenDefaultOrOptional = System.Logger.Level.DEBUG;

// Java date decoder format.
private DateTimeFormatter dateDecoderFormat = DateTimeFormatter.ISO_DATE_TIME;
Expand Down Expand Up @@ -113,6 +115,25 @@ public void setTreatNullValuesInClassAsErrors(boolean treatNullValuesInClassAsEr
this.treatNullValuesInClassAsErrors = treatNullValuesInClassAsErrors;
}


/**
* Provide the log level when we log a message when a config is missing, but we provided a default, or it is Optional.
*
* @return Log level
*/
public System.Logger.Level getLogLevelForMissingValuesWhenDefaultOrOptional() {
return logLevelForMissingValuesWhenDefaultOrOptional;
}

/**
* Provide the log level when we log a message when a config is missing, but we provided a default, or it is Optional.
*
* @param logLevelForMissingValuesWhenDefaultOrOptional Log level
*/
public void setLogLevelForMissingValuesWhenDefaultOrOptional(System.Logger.Level logLevelForMissingValuesWhenDefaultOrOptional) {
this.logLevelForMissingValuesWhenDefaultOrOptional = logLevelForMissingValuesWhenDefaultOrOptional;
}

/**
* Java date decoder format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ public static class ArrayMissingIndex extends ValidationError {
private final String path;

public ArrayMissingIndex(long index) {
super(ValidationLevel.WARN);
super(ValidationLevel.MISSING_VALUE);
this.index = index;
this.path = null;
}

public ArrayMissingIndex(long index, String path) {
super(ValidationLevel.WARN);
super(ValidationLevel.MISSING_VALUE);
this.index = index;
this.path = path;
}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ public String description() {
}

/**
* Transform doesnt match the regex
* Transform doesnt match the regex.
*/
public static class TransformDoesntMatchRegex extends ValidationError {
private final String path;
Expand All @@ -1135,7 +1135,7 @@ public String description() {
}

/**
* Not a valid SubstitutionNode
* Not a valid SubstitutionNode.
*/
public static class NotAValidSubstitutionNode extends ValidationError {
private final String path;
Expand All @@ -1155,7 +1155,7 @@ public String description() {


/**
* Not a valid SubstitutionNode
* Not a valid SubstitutionNode.
*/
public static class ExceededMaximumNestedSubstitutionDepth extends ValidationError {
private final String path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ protected ValidateOf<ConfigNode> buildConfigTree(List<Pair<List<Token>, ConfigVa
errorList.addAll(recursiveErrors.get(ValidationLevel.WARN));
}

// add all missing values errors to the current error list.
if (recursiveErrors.containsKey(ValidationLevel.MISSING_VALUE)) {
errorList.addAll(recursiveErrors.get(ValidationLevel.MISSING_VALUE));
}

// if there are any error level return immediately unless we have treatErrorsAsWarnings enabled.
if (recursiveErrors.containsKey(ValidationLevel.ERROR)) {
errorList.addAll(recursiveErrors.get(ValidationLevel.ERROR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public void testMergeArraysMissingIndex() throws GestaltException {
} catch (GestaltException e) {
assertThat(e).isInstanceOf(GestaltException.class)
.hasMessage("Failed getting config path: admin.user, for class: java.util.List<java.lang.String>\n" +
" - level: WARN, message: Missing array index: 2");
" - level: MISSING_VALUE, message: Missing array index: 2");
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void build() throws GestaltException {
.setDecoders(decoders)
.addDecoder(new LongDecoder())
.setTreatWarningsAsErrors(true)
.setLogLevelForMissingValuesWhenDefaultOrOptional(System.Logger.Level.DEBUG)
.setSubstitutionOpeningToken("${")
.setSubstitutionClosingToken("}")
.setMaxSubstitutionNestedDepth(5)
.setSubstitutionRegex("")
.setGestaltConfig(new GestaltConfig())
.setConfigLoaderService(new ConfigLoaderRegistry())
.addConfigLoader(new MapConfigLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ public void testArrayIndexMissing() {
assertNotNull(validateOfResults.results());
List<ValidationError> errors = validateOfResults.getErrors();
assertEquals(2, errors.size());
assertEquals(ValidationLevel.WARN, errors.get(0).level());
assertEquals(ValidationLevel.MISSING_VALUE, errors.get(0).level());
assertEquals("Missing array index: 1 for path: db.hosts", errors.get(0).description());
assertEquals(ValidationLevel.WARN, errors.get(1).level());
assertEquals(ValidationLevel.MISSING_VALUE, errors.get(1).level());
assertEquals("Missing array index: 3 for path: db.hosts", errors.get(1).description());

ConfigNode results = validateOfResults.results();
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ weld = "3.1.0.Final"
weldCore = "4.0.3.Final"

hocon = "1.4.2"
aws = "2.20.52"
aws = "2.20.55"
jgit = "6.5.0.202303070854-r"

eddsa = "0.3.0"

junit5 = "5.9.2"
junit5 = "5.9.3"
assertJ = "3.24.2"
mockito = "5.2.0"
mockk = "1.13.5"
Expand Down

0 comments on commit 905cc98

Please sign in to comment.