<?xml version="1.0"?>

<ruleset name="Java Logging"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

  <description>
The Java Logging ruleset contains a collection of rules that find questionable usages of the logger.
  </description>

    <rule name="MoreThanOneLogger"
    	   since="2.0"
         message="Class contains more than one logger."
         class="net.sourceforge.pmd.lang.java.rule.logging.MoreThanOneLoggerRule"
          externalInfoUrl="${pmd.website.baseurl}/rules/java/logging-java.html#MoreThanOneLogger">
     <description>
Normally only one logger is used in each class.
     </description>
        <priority>2</priority>
     <example>
 <![CDATA[
public class Foo {
    Logger log = Logger.getLogger(Foo.class.getName());
    // It is very rare to see two loggers on a class, normally
    // log information is multiplexed by levels
    Logger log2= Logger.getLogger(Foo.class.getName());
}
]]>
     </example>
     </rule>

     <rule name="LoggerIsNotStaticFinal"
   		language="java"
     		since="2.0"
         message="The Logger variable declaration does not contain the static and final modifiers"
         class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="${pmd.website.baseurl}/rules/java/logging-java.html#LoggerIsNotStaticFinal">
     <description>
In most cases, the Logger reference can be declared as static and final.
     </description>
     <priority>2</priority>
     <properties>
         <property name="xpath">
             <value>
                 <![CDATA[
//VariableDeclarator
 [parent::FieldDeclaration]
 [../Type/ReferenceType
  /ClassOrInterfaceType[@Image='Logger']
   and
  (..[@Final='false'] or ..[@Static = 'false'] ) ]
                ]]>
             </value>
         </property>
     </properties>
     <example>
 <![CDATA[
public class Foo{
    Logger log = Logger.getLogger(Foo.class.getName());					// not recommended

    static final Logger log = Logger.getLogger(Foo.class.getName());	// preferred approach
}
]]>
     </example>
   </rule>

    <rule name="SystemPrintln"
   		language="java"
    		since="2.1"
         message="{0} is used"
         class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="${pmd.website.baseurl}/rules/java/logging-java.html#SystemPrintln">
     <description>
References to System.(out|err).print are usually intended for debugging purposes and can remain in
the codebase even in production code. By using a logger one can enable/disable this behaviour at
will (and by priority) and avoid clogging the Standard out log.
     </description>
     <priority>2</priority>
     <properties>
         <property name="xpath">
             <value>
                 <![CDATA[
//Name[
    starts-with(@Image, 'System.out.print')
    or
    starts-with(@Image, 'System.err.print')
    ]
                ]]>
             </value>
         </property>
     </properties>
     <example>
 <![CDATA[
class Foo{
    Logger log = Logger.getLogger(Foo.class.getName());
    public void testA () {
        System.out.println("Entering test");
        // Better use this
        log.fine("Entering test");
    }
}
]]>
     </example>
     </rule>

    <rule  name="AvoidPrintStackTrace"
   		language="java"
    		  since="3.2"
           message="Avoid printStackTrace(); use a logger call instead."
           class="net.sourceforge.pmd.lang.rule.XPathRule"
		   externalInfoUrl="${pmd.website.baseurl}/rules/java/logging-java.html#AvoidPrintStackTrace">
           <description>
Avoid printStackTrace(); use a logger call instead.
           </description>
           <priority>3</priority>
           <properties>
             <property name="xpath">
             <value>
<![CDATA[
//PrimaryExpression
 [PrimaryPrefix/Name[contains(@Image,'printStackTrace')]]
 [PrimarySuffix[not(boolean(Arguments/ArgumentList/Expression))]]
]]>
             </value>
             </property>
           </properties>
           <example>
<![CDATA[
class Foo {
  void bar() {
    try {
     // do something
    } catch (Exception e) {
     e.printStackTrace();
     }
   }
}
]]>
           </example>
    </rule>

   <rule name="GuardLogStatementJavaUtil"
         language="java"
         since="5.1.0"
         message="There is log block not surrounded by if"
         class="net.sourceforge.pmd.lang.java.rule.logging.GuardLogStatementJavaUtilRule"
         externalInfoUrl="${pmd.website.baseurl}/rules/java/logging-java.html#GuardLogStatementJavaUtil">
     <description>
Whenever using a log level, one should check if the loglevel is actually enabled, or
otherwise skip the associate String creation and manipulation.
     </description>
     <priority>2</priority>
     <example>
 <![CDATA[
 	// Add this for performance
	if (log.isLoggable(Level.FINE)) { ...
 	    log.fine("log something" + " and " + "concat strings");
]]>
     </example>
   </rule>
</ruleset>
