The eleventh installation of explanation of PMD rules covering Jakarta Common Logging Rules.
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.
Example
ProperLogger
A logger should normally be defined private static final and have the correct class. Private final Log log; is also allowed for rare cases where loggers need to be passed around, with the restriction that the logger needs to be passed into the constructor.
Example
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.Example
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
A logger should normally be defined private static final and have the correct class. Private final Log log; is also allowed for rare cases where loggers need to be passed around, with the restriction that the logger needs to be passed into the constructor.Example
public class Foo {
// right
private static final Log LOG = LogFactory.getLog(Foo.class);
// wrong
protected Log LOG = LogFactory.getLog(Testclass.class);
}
No comments:
Post a Comment