##### Overview

- Download PMD 3.7
- What's new in PMD 3.7
- PMD in the news
- PMD-related products and books
- Best practices
- Future directions
- Similar projects
- Credits
- License
- What does 'PMD' mean?

##### Usage

- Installation
- Command line usage
- Ant task usage
- Maven plugin usage
- IDE plugin usage
- Suppressing warnings
- Finding duplicated code
- JSP support

##### Customizing PMD

- Compiling PMD
- How to write a rule
- Writing XPath rules
- How to make a rule set
- How it works

##### For example

- Run PMD on a Sourceforge project

##### Rule Sets

- Index
- Basic
- Braces
- Code Size
- Clone
- Controversial
- Coupling
- Design
- Finalizers
- Import Statements
- J2EE
- Javabeans
- JUnit Tests
- Logging (Java)
- Logging (Jakarta)
- Migrating
- Naming
- Optimizations
- Strict Exceptions
- Strings
- Sun Security
- Unused Code
- Java Server Pages
- Java Server Faces

##### Project Documentation

- About PMD
- Project Info
- Project Reports
- Development Process

## Jakarta Commons Logging Rules

The Jakarta Commons Logging ruleset contains a collection of rules that find questionable usages of that framework.

### UseCorrectExceptionLogging

To make sure the full stacktrace is printed out, use the logging statement with 2 arguments: a String and a Throwable.

This rule is defined by the following XPath expression:

    //CatchStatement/Block/BlockStatement/Statement/StatementExpression
    /PrimaryExpression[PrimaryPrefix/Name[starts-with(@Image,
    concat(//ClassOrInterfaceBodyDeclaration/FieldDeclaration
    [Type//ClassOrInterfaceType[@Image='Log']]
    /VariableDeclarator/VariableDeclaratorId/@Image, '.'))]]
    [PrimarySuffix/Arguments[@ArgumentCount='1']]
    [PrimarySuffix/Arguments//Name/@Image = ancestor::CatchStatement/FormalParameter/VariableDeclaratorId/@Image]

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

    public class Main {
     private static final Log _LOG = LogFactory.getLog( Main.class );
     void bar() {
      try {
      } catch( Exception e ) {
       _LOG.error( e ); //Wrong!
      } catch( OtherException oe ) {
       _LOG.error( oe.getMessage(), oe ); //Correct
      }
     }
    }

### ProperLogger

Logger should normally be defined private static final and have the correct class. Private final Log log; is also allowed for rare cases when loggers need to be passed around, but the logger needs to be passed into the constructor.

This rule is defined by the following XPath expression:

     //ClassOrInterfaceBodyDeclaration[FieldDeclaration//ClassOrInterfaceType[@Image='Log']
     and
     not(FieldDeclaration[@Final='true'][@Static='true'][@Private='true'][//VariableDeclaratorId[@Image=$staticLoggerName]]
     //ArgumentList//ClassOrInterfaceType/@Image = ancestor::ClassOrInterfaceDeclaration/@Image)
     and
     not(FieldDeclaration[@Final='true'][@Private='true'][//VariableDeclaratorId[@Image='log']]
     [count(//VariableInitializer)=0]
     [/descendant::StatementExpression[//PrimaryExpression/descendant::*[@Image='log']][count(//AllocationExpression)=0]]
     )]

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

     public class Foo {
     // right
      private static final Log LOG = LogFactory.getLog(Foo.class);
     // wrong
     protected Log LOG = LogFactory.getLog(Testclass.class);
    }

This rule has the following properties:

Name | Default value | Description
staticLoggerName |  |