Java Logging Rules

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

MoreThanOneLogger

Normally only one logger is used in each class.

This rule is defined by the following XPath expression:

                 
//ClassOrInterfaceBody [
 count(
  //VariableDeclarator[../Type/ReferenceType/ClassOrInterfaceType[@Image='Logger']]
  )>1
]                
             

Here's an example of code that would trigger this rule:

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

     
		

LoggerIsNotStaticFinal

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

This rule is defined by the following XPath expression:

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

Here's an example of code that would trigger this rule:

			
 
class Foo{
    Logger log = Logger.getLogger(Foo.class.getName());
    // It is much better to declare the logger as follows 
    // static final Logger log = Logger.getLogger(Foo.class.getName());
}

     
		

SystemPrintln

System.(out|err).print is used, consider using a logger.

This rule is defined by the following XPath expression:

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

Here's an example of code that would trigger this rule:

			
 
class Foo{
    Logger log = Logger.getLogger(Foo.class.getName());

    public void testA () {
        System.out.println("Entering test");
        // Better use this
        log.fine("Entering test");
    }
}