Fork me on GitHub

Java Logging

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

MoreThanOneLogger

Since: PMD 2.0

Priority: 2

Normally only one logger is used in each class.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.logging.MoreThanOneLoggerRule

Example(s):

 
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());
}

     

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.

LoggerIsNotStaticFinal

Since: PMD 2.0

Priority: 2

In most cases, the Logger reference can be declared as static and final.

                 
//VariableDeclarator
 [parent::FieldDeclaration]
 [../Type/ReferenceType
  /ClassOrInterfaceType[@Image='Logger']
   and
  (..[@Final='false'] or ..[@Static = 'false'] ) ]
                
             

Example(s):

 
public class Foo{
    Logger log = Logger.getLogger(Foo.class.getName());					// not recommended

    static final Logger log = Logger.getLogger(Foo.class.getName());	// preferred approach
}

     

SystemPrintln

Since: PMD 2.1

Priority: 2

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.

                 
//Name[
    starts-with(@Image, 'System.out.print')
    or
    starts-with(@Image, 'System.err.print')
    ]
                
             

Example(s):

 
class Foo{
    Logger log = Logger.getLogger(Foo.class.getName());
    public void testA () {
        System.out.println("Entering test");
        // Better use this
        log.fine("Entering test");
    }
}

     

AvoidPrintStackTrace

Since: PMD 3.2

Priority: 3

Avoid printStackTrace(); use a logger call instead.


//PrimaryExpression
 [PrimaryPrefix/Name[contains(@Image,'printStackTrace')]]
 [PrimarySuffix[not(boolean(Arguments/ArgumentList/Expression))]]

             

Example(s):


class Foo {
  void bar() {
    try {
     // do something
    } catch (Exception e) {
     e.printStackTrace();
     }
   }
}

           

GuardLogStatementJavaUtil

Since: PMD 5.1.0

Priority: 2

Whenever using a log level, one should check if the loglevel is actually enabled, or otherwise skip the associate String creation and manipulation.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.logging.GuardLogStatementJavaUtilRule

Example(s):

 
 	// Add this for performance
	if (log.isLoggable(Level.FINE)) { ...
 	    log.fine("log something" + " and " + "concat strings");

     

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.
guardsMethods [] method use to guard the log statement
logLevels [] LogLevels to guard

InvalidSlf4jMessageFormat

Since: PMD 5.5.0

Priority: 5

Check for messages in slf4j loggers with non matching number of arguments and placeholders.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.logging.InvalidSlf4jMessageFormatRule

Example(s):

                
LOGGER.error("forget the arg {}");
LOGGER.error("too many args {}", "arg1", "arg2");
LOGGER.error("param {}", "arg1", new IllegalStateException("arg")); //The exception is shown separately, so is correct.

     

This rule has the following properties:

Name Default Value Description
violationSuppressRegex Suppress violations with messages matching a regular expression
violationSuppressXPath Suppress violations on nodes which match a given relative XPath expression.