Applied patch 3130615: New Rule GuardDebugLogging for jakarta-commons logging. Thanks to Tammo van Lessen.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.3.x@7359 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse 2011-09-29 13:59:25 +00:00
parent d0f2ec451a
commit 791ddbef0b
4 changed files with 50 additions and 1 deletions

View File

@ -40,6 +40,7 @@ New rule:
StrictExceptions : AvoidCatchingGenericException, AvoidLosingExceptionInformation
Naming : GenericsNaming
JSP: NoInlineScript
Logging with Jakarta Commons ruleset: GuardDebugLogging
February 08, 2009 - 4.2.5:

View File

@ -10,6 +10,7 @@ public class LoggingJakartaCommonsRulesTest extends SimpleAggregatorTst {
public void setUp() {
addRule("logging-jakarta-commons", "ProperLogger");
addRule("logging-jakarta-commons", "UseCorrectExceptionLogging");
addRule("logging-jakarta-commons", "GuardDebugLogging");
}
public static junit.framework.Test suite() {

View File

@ -89,5 +89,51 @@ around, with the restriction that the logger needs to be passed into the constru
</example>
</rule>
<rule name="GuardDebugLogging"
since="4.3"
message="debug logging that involves string concatenation should be guarded with isDebugEnabled() checks"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/logging-jakarta-commons.html#GuardDebugLogging">
<description>
When log messages are composed by concatenating strings, the whole section should be guarded
by a isDebugEnabled() check to avoid performance and memory issues.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix[ends-with(Name/@Image, '.debug') and count(../descendant::AdditiveExpression) > 0 and count(ancestor::IfStatement/descendant::PrimaryExpression[ends-with(descendant::PrimaryPrefix/Name/@Image, 'isDebugEnabled')]) = 0]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Test {
private static final Log __log = LogFactory.getLog(Test.class);
public void test() {
// okay:
__log.debug("log something");
// okay:
__log.debug("log something with exception", e);
// bad:
__log.debug("log something" + " and " + "concat strings");
// bad:
__log.debug("log something" + " and " + "concat strings", e);
// good:
if (__log.isDebugEnabled()) {
__log.debug("bla" + "",e );
}
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -321,6 +321,7 @@
<li>Lucian Ciufudean - RedundantFieldInitializerRule</li>
<li>Andreas Dangel - GodClass and LawOfDemeter rules, several bugfixes and cleanup</li>
<li>Riku Nykanen - patch improving TooManyMethods rule</li>
<li>Tammo van Lessen - new rule GuardDebugLogging for Jakarta Commons Logging ruleset.</li>
</ul>
</subsection>
<subsection name="Organizations">