Skip to content

Commit

Permalink
Enhanced logging to include the constraint identifier if it exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 3, 2024
1 parent 0fc09a6 commit 6259531
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

/**
* Supports logging constraint findings to the configured Log4J2 instance.
*/
public class LoggingConstraintValidationHandler
extends AbstractConstraintValidationHandler {
private static final Logger LOGGER = LogManager.getLogger(DefaultConstraintValidator.class);
private static final Throwable NO_EXCEPTION = null;

private static LogBuilder getLogBuilder(@NonNull Level level) {
LogBuilder retval;
Expand Down Expand Up @@ -84,17 +88,21 @@ private static boolean isLogged(@NonNull Level level) {
return retval;
}

private void logConstraint(
private void logMessage(
@NonNull Level level,
@Nullable String identifier,
@NonNull INodeItem node,
@NonNull CharSequence message,
@Nullable Throwable cause) {
LogBuilder builder = getLogBuilder(level);
if (cause != null) {
builder.withThrowable(cause);
}

builder.log("{}: ({}) {}", level.name(), toPath(node), message);
builder.log("{}{}: ({}) {}",
identifier == null ? "" : "[" + identifier + "] ",
level.name(),
toPath(node),
message);
}

@Override
Expand All @@ -105,15 +113,16 @@ public void handleCardinalityMinimumViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
node,
newCardinalityMinimumViolationMessage(
constraint,
node,
targets,
dynamicContext),
null); // Null because there is no exeception, a Throwable cause.
NO_EXCEPTION); // Null because there is no exeception, a Throwable cause.
}
}

Expand All @@ -125,8 +134,9 @@ public void handleCardinalityMaximumViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
node,
newCardinalityMaximumViolationMessage(
constraint,
Expand All @@ -146,16 +156,17 @@ public void handleIndexDuplicateKeyViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
target,
newIndexDuplicateKeyViolationMessage(
constraint,
node,
oldItem,
target,
dynamicContext),
null);
NO_EXCEPTION);
}
}

Expand All @@ -168,16 +179,17 @@ public void handleUniqueKeyViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
target,
newUniqueKeyViolationMessage(
constraint,
node,
oldItem,
target,
dynamicContext),
null);
NO_EXCEPTION);
}
}

Expand All @@ -191,7 +203,12 @@ public void handleKeyMatchError(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(level, target, cause.getLocalizedMessage(), cause);
logMessage(
level,
constraint.getId(),
target,
cause.getLocalizedMessage(),
cause);
}
}

Expand All @@ -205,8 +222,9 @@ public void handleMatchPatternViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
target,
newMatchPatternViolationMessage(
constraint,
Expand All @@ -215,7 +233,7 @@ public void handleMatchPatternViolation(
value,
pattern,
dynamicContext),
null);
NO_EXCEPTION);
}
}

Expand All @@ -230,8 +248,9 @@ public void handleMatchDatatypeViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
target,
newMatchDatatypeViolationMessage(
constraint,
Expand All @@ -252,15 +271,16 @@ public void handleExpectViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
target,
newExpectViolationMessage(
constraint,
node,
target,
dynamicContext),
null);
NO_EXCEPTION);
}
}

Expand All @@ -275,8 +295,9 @@ public void handleAllowedValuesViolation(
.max(Comparator.comparing(Level::ordinal))
.get());
if (isLogged(level)) {
logConstraint(
logMessage(
level,
null,
target,
newAllowedValuesViolationMessage(
failedConstraints,
Expand All @@ -293,13 +314,14 @@ public void handleIndexDuplicateViolation(
// always log at level critical
Level level = Level.CRITICAL;
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
node,
newIndexDuplicateViolationMessage(
constraint,
node),
null);
NO_EXCEPTION);
}
}

Expand All @@ -312,16 +334,17 @@ public void handleIndexMiss(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
node,
newIndexMissMessage(
constraint,
node,
target,
key,
dynamicContext),
null);
NO_EXCEPTION);
}
}

Expand All @@ -334,16 +357,17 @@ public void handleMissingIndexViolation(
@NonNull DynamicContext dynamicContext) {
Level level = constraint.getLevel();
if (isLogged(level)) {
logConstraint(
logMessage(
level,
constraint.getId(),
node,
newMissingIndexViolationMessage(
constraint,
node,
target,
message,
dynamicContext),
null);
NO_EXCEPTION);
}
}

Expand All @@ -353,7 +377,14 @@ public void handlePass(
@NonNull INodeItem node,
@NonNull INodeItem target,
@NonNull DynamicContext dynamicContext) {
// do nothing
if (LOGGER.isDebugEnabled()) {
String identifier = constraint.getId();
LOGGER.atDebug().log("{}{}: ({}) {}",
identifier == null ? "" : "[" + identifier + "] ",
Level.INFORMATIONAL.name(),
toPath(node),
"Passed");
}
}

@Override
Expand All @@ -365,7 +396,12 @@ public void handleError(
@NonNull DynamicContext dynamicContext) {
Level level = Level.CRITICAL;
if (isLogged(level)) {
logConstraint(level, node, message, exception);
logMessage(
level,
constraint.getId(),
node,
message,
exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import gov.nist.secauto.metaschema.core.model.AbstractModuleLoader;
import gov.nist.secauto.metaschema.core.model.IModuleLoader;
import gov.nist.secauto.metaschema.core.model.MetaschemaException;
import gov.nist.secauto.metaschema.core.model.constraint.ExternalConstraintsModulePostProcessor;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
import gov.nist.secauto.metaschema.core.model.xml.impl.XmlModule;
import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.METASCHEMADocument;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
Expand All @@ -21,6 +23,7 @@

import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -47,7 +50,18 @@ public class ModuleLoader
* Construct a new Metaschema loader.
*/
public ModuleLoader() {
this(CollectionUtil.emptyList());
this(CollectionUtil.<IModuleLoader.IModulePostProcessor>emptyList());
}

/**
* Construct a new Metaschema loader, which applies the provided constraints to
* loaded modules.
*
* @param constraints
* a set of Metaschema module constraints
*/
public ModuleLoader(@NonNull Collection<IConstraintSet> constraints) {
this(CollectionUtil.singletonList(new ExternalConstraintsModulePostProcessor(constraints)));
}

/**
Expand Down

0 comments on commit 6259531

Please sign in to comment.