-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a New Logging Rule checking logger consistent usage
Closes gh-52
- Loading branch information
mnhock
authored and
mnhock
committed
Jun 21, 2024
1 parent
f45b700
commit df14804
Showing
10 changed files
with
211 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/enofex/taikai/logging/LoggerConventions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.enofex.taikai.logging; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.JavaField; | ||
import com.tngtech.archunit.core.domain.JavaModifier; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import com.tngtech.archunit.lang.ConditionEvents; | ||
import com.tngtech.archunit.lang.SimpleConditionEvent; | ||
import java.util.Set; | ||
|
||
final class LoggerConventions { | ||
|
||
private LoggerConventions() { | ||
} | ||
|
||
static ArchCondition<JavaClass> followLoggerConventions(String typeName, String regex, | ||
Set<JavaModifier> requiredModifiers) { | ||
return new ArchCondition<>( | ||
"have a logger field of type %s with name pattern %s and modifiers %s".formatted( | ||
typeName, regex, requiredModifiers)) { | ||
@Override | ||
public void check(JavaClass javaClass, ConditionEvents events) { | ||
for (JavaField field : javaClass.getAllFields()) { | ||
if (field.getRawType().isAssignableTo(typeName)) { | ||
if (!field.getName().matches(regex)) { | ||
events.add(SimpleConditionEvent.violated(field, | ||
"Field '%s' in class %s does not match the naming pattern '%s'".formatted( | ||
field.getName(), | ||
javaClass.getName(), regex))); | ||
} | ||
|
||
if (!field.getModifiers().containsAll(requiredModifiers)) { | ||
events.add(SimpleConditionEvent.violated(field, | ||
"Field '%s' in class %s does not have the required modifiers %s".formatted( | ||
field.getName(), | ||
javaClass.getName(), | ||
requiredModifiers))); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/enofex/taikai/logging/LoggingConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.enofex.taikai.logging; | ||
|
||
import static com.enofex.taikai.logging.LoggerConventions.followLoggerConventions; | ||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
|
||
import com.enofex.taikai.TaikaiRule; | ||
import com.enofex.taikai.TaikaiRule.Configuration; | ||
import com.enofex.taikai.configures.AbstractConfigurer; | ||
import com.enofex.taikai.configures.ConfigurerContext; | ||
import com.tngtech.archunit.core.domain.JavaModifier; | ||
import java.util.Set; | ||
|
||
public final class LoggingConfigurer extends AbstractConfigurer { | ||
|
||
public LoggingConfigurer(ConfigurerContext configurerContext) { | ||
super(configurerContext); | ||
} | ||
|
||
public LoggingConfigurer loggersShouldFollowConventions(String typeName, String regex, | ||
Set<JavaModifier> requiredModifiers) { | ||
return loggersShouldFollowConventions(typeName, regex, requiredModifiers, null); | ||
} | ||
|
||
public LoggingConfigurer loggersShouldFollowConventions(String typeName, String regex, | ||
Set<JavaModifier> requiredModifiers, Configuration configuration) { | ||
return addRule(TaikaiRule.of(classes().should( | ||
followLoggerConventions(typeName, regex, requiredModifiers)), | ||
configuration)); | ||
} | ||
|
||
public LoggingConfigurer loggersShouldFollowConventions(Class<?> clazz, String regex, | ||
Set<JavaModifier> requiredModifiers) { | ||
return loggersShouldFollowConventions(clazz, regex, requiredModifiers, null); | ||
} | ||
|
||
public LoggingConfigurer loggersShouldFollowConventions(Class<?> clazz, String regex, | ||
Set<JavaModifier> requiredModifiers, Configuration configuration) { | ||
return addRule(TaikaiRule.of(classes().should( | ||
followLoggerConventions(clazz.getName(), regex, requiredModifiers)), | ||
configuration)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.