-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rule to limit excessive use of sequential loggers #56
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5e881f9
PomDependencyVersionRule Implementation.
5f0a674
Refactor for Plugin and Dependency. Resolve PR Comments.
b856541
Message change.
e5f0c62
Add rule to limit excessive use of sequential loggers
MrMcCartney 5ce2f27
Add component count rule
MrMcCartney 49a7816
Add Until Successful variant of component count rule
MrMcCartney b5f7396
Merge pull request #51 from avioconsulting/Feature/#39-PomDependencyV…
thedevnisarg a2647cc
Switch LoggerComponent level to use an enum.
MrMcCartney 54a4d3f
Add component count rule
MrMcCartney ed00b55
Add Until Successful variant of component count rule
MrMcCartney 6d22c38
Add Namespace file with list of common namespaces
MrMcCartney dc35147
Merge branch 'Feature/#46-component-count-rule' of github.com:aviocon…
MrMcCartney 5959a9c
Merge pull request #57 from avioconsulting/Feature/#46-component-coun…
MrMcCartney f8358e0
Add rule to limit excessive use of sequential loggers
MrMcCartney 0a677ea
Switch LoggerComponent level to use an enum.
MrMcCartney a8f3297
Change constructor to use EnumMap rather than Map
MrMcCartney 000788e
Merge branch 'Feature/#44-excessive-loggers' of github.com:avioconsul…
MrMcCartney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...main/groovy/com/avioconsulting/mule/linter/rule/configuration/ExcessiveLoggersRule.groovy
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,66 @@ | ||
package com.avioconsulting.mule.linter.rule.configuration | ||
|
||
import com.avioconsulting.mule.linter.model.Application | ||
import com.avioconsulting.mule.linter.model.configuration.FlowComponent | ||
import com.avioconsulting.mule.linter.model.rule.Rule | ||
import com.avioconsulting.mule.linter.model.rule.RuleViolation | ||
|
||
class ExcessiveLoggersRule extends Rule { | ||
static final String RULE_ID = 'EXCESSIVE_LOGGERS_RULE' | ||
static final String RULE_NAME = 'Excessive use of Sequential Loggers' | ||
static final String RULE_VIOLATION_MESSAGE = 'Too many sequential loggers of same level in flow ' | ||
|
||
Map<String, Integer> excessiveLoggers = ["TRACE": 2, "DEBUG": 2, "INFO": 2, "WARN": 2, "ERROR": 2] | ||
|
||
ExcessiveLoggersRule() {} | ||
|
||
ExcessiveLoggersRule(Integer excessiveLoggers) { | ||
this(["TRACE": excessiveLoggers, "DEBUG": excessiveLoggers, "INFO": excessiveLoggers, | ||
"WARN": excessiveLoggers, "ERROR": excessiveLoggers]) | ||
} | ||
|
||
ExcessiveLoggersRule(Map<String, Integer> excessiveLoggers) { | ||
this.ruleId = RULE_ID | ||
this.ruleName = RULE_NAME | ||
this.excessiveLoggers.putAll excessiveLoggers | ||
} | ||
|
||
@Override | ||
List<RuleViolation> execute(Application application) { | ||
List<RuleViolation> violations = [] | ||
application.flows.each { FlowComponent flow -> | ||
RuleViolation violation = searchFlowForLoggers(flow) | ||
if (violation != null) { | ||
violations.add(violation) | ||
} | ||
} | ||
application.subFlows.each { FlowComponent flow -> | ||
RuleViolation violation = searchFlowForLoggers(flow) | ||
if (violation != null) { | ||
violations.add(violation) | ||
} | ||
} | ||
return violations | ||
} | ||
|
||
private RuleViolation searchFlowForLoggers(FlowComponent flow) { | ||
String logLevel = null | ||
Integer count = 0 | ||
RuleViolation violation = null | ||
flow.children.each { | ||
if (it.componentName == "logger") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use COMPONENT_NAME from the LoggerComponent model class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Easy enough |
||
if (it.getAttributeValue("level") == logLevel) { | ||
count++ | ||
if (count >= excessiveLoggers.get(it.getAttributeValue("level"))) { | ||
violation = new RuleViolation(this, flow.file.path, flow.lineNumber, RULE_VIOLATION_MESSAGE | ||
+ flow.getAttributeValue("name")) | ||
} | ||
} else { | ||
logLevel = it.getAttributeValue("level") | ||
count = 1 | ||
} | ||
} | ||
} | ||
return violation | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
.../groovy/com/avioconsulting/mule/linter/rule/configuration/ExcessiveLoggersRuleTest.groovy
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,84 @@ | ||
package com.avioconsulting.mule.linter.rule.configuration | ||
|
||
import com.avioconsulting.mule.linter.TestApplication | ||
import com.avioconsulting.mule.linter.model.Application | ||
import com.avioconsulting.mule.linter.model.rule.Rule | ||
import com.avioconsulting.mule.linter.model.rule.RuleViolation | ||
import spock.lang.Specification | ||
|
||
class ExcessiveLoggersRuleTest extends Specification{ | ||
private final TestApplication testApp = new TestApplication() | ||
|
||
def setup() { | ||
testApp.initialize() | ||
testApp.addPom() | ||
testApp.buildConfigContent('business-logic.xml', LOGGERS) | ||
} | ||
|
||
def cleanup() { | ||
testApp.remove() | ||
} | ||
|
||
def 'Default excessive loggers rule fails two flows'() { | ||
given: | ||
Rule rule = new ExcessiveLoggersRule() | ||
|
||
when: | ||
Application app = new Application(testApp.appDir) | ||
List<RuleViolation> violations = rule.execute(app) | ||
|
||
then: | ||
violations.size() == 2 | ||
violations[0].message == ExcessiveLoggersRule.RULE_VIOLATION_MESSAGE + | ||
"get:\\user\\(id)\\roles:application\\json:my-api-config" | ||
violations[1].message == ExcessiveLoggersRule.RULE_VIOLATION_MESSAGE + "business-subflow-three" | ||
} | ||
|
||
def '3 Count excessive loggers rule fails one flow'() { | ||
given: | ||
Rule rule = new ExcessiveLoggersRule(3) | ||
|
||
when: | ||
Application app = new Application(testApp.appDir) | ||
List<RuleViolation> violations = rule.execute(app) | ||
|
||
then: | ||
violations.size() == 1 | ||
violations[0].message == ExcessiveLoggersRule.RULE_VIOLATION_MESSAGE + "business-subflow-three" | ||
} | ||
|
||
def 'Custom excessive loggers rule fails no flows'() { | ||
given: | ||
Rule rule = new ExcessiveLoggersRule(["TRACE": 2, "DEBUG": 3, "INFO": 3, "WARN": 2, "ERROR": 4]) | ||
|
||
when: | ||
Application app = new Application(testApp.appDir) | ||
List<RuleViolation> violations = rule.execute(app) | ||
|
||
then: | ||
violations.size() == 0 | ||
} | ||
|
||
private static final String LOGGERS = ''' | ||
\t<flow name="get:\\user\\(id)\\roles:application\\json:my-api-config" doc:id="bcadf69e-4504-4654-9c7a-38971a01ed11" > | ||
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298a" /> | ||
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298b" /> | ||
\t\t<logger level="DEBUG" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298c" /> | ||
\t\t<logger level="DEBUG" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298d" /> | ||
\t\t<flow-ref doc:name="Flow Reference" doc:id="4bc8fa65-90c4-454f-b5b7-c1c318f3a1cb" name="business-subflow-two"/> | ||
\t</flow> | ||
|
||
\t<sub-flow name="business-subflow-two"> | ||
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297a" /> | ||
\t\t<logger level="DEBUG" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297b" /> | ||
\t\t<logger level="WARN" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297c" /> | ||
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297d" /> | ||
\t</sub-flow> | ||
|
||
\t<sub-flow name="business-subflow-three"> | ||
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297a" /> | ||
\t\t<logger level="ERROR" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297b" /> | ||
\t\t<logger level="ERROR" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297c" /> | ||
\t\t<logger level="ERROR" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297d" /> | ||
\t</sub-flow>''' | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move these strings to either be an enum or static constants, probably not on this class but maybe on the LoggerComponent model class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll try an Enum for now.