This ruleset helps you to code with SLF4J.
This project has 2 sub modules.
Simple PMD rules and default ruleset for SLF4J. You can use default ruleset easily with Maven like below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.7.1</version>
<dependencies>
<dependency>
<groupId>jp.skypencil</groupId>
<artifactId>RuleSet-for-SLF4J</artifactId>
<version>0.4</version>
</dependency>
</dependencies>
<configuration>
<linkXRef>false</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<targetJdk>1.6</targetJdk>
<rulesets>
<ruleset>slf4j.xml</ruleset><!-- to use default ruleset for SLF4J -->
</rulesets>
</configuration>
</plugin>
If you want to use a part of these rules, you have to create your ruleset XML file. Sample ruleset XML file is here.
An plugin for Sonar.
Please put this plugin (.jar) to your ${sonar.home}/extensions/plugin
directory.
You can command mvn package
to build jar file, or download it from Maven central.
Currently, we've provided 5 rules.
Given argument for LoggerFactory#getLogger(Class) is wrong. You have to provide the Class which you're coding.
class Foo {
// You have to avoid passing Bar.class in Foo.class
private final Logger bad = LoggerFactory.getLogger(Bar.class);
// This is OK
private final Logger logger = LoggerFactory.getLogger(Foo.class);
}
Logger should be private.
class Foo {
// You have to avoid publishing Logger
public final Logger bad = LoggerFactory.getLogger(Foo.class);
// This is OK
private final Logger logger = LoggerFactory.getLogger(Foo.class);
}
Logger should be final.
class Foo {
// Logger should be final
private Logger bad = LoggerFactory.getLogger(Foo.class);
// This is OK
private final Logger logger = LoggerFactory.getLogger(Foo.class);
}
Sometimes using non-static logger is better than using static one. See official FAQ for detail. If you want to keep your logger static, please use default logging rule instead.
class Foo {
// Logger has to be non-static
private final static Logger bad = LoggerFactory.getLogger(Foo.class);
// This is OK
private final Logger logger = LoggerFactory.getLogger(Foo.class);
}
class Foo {
private final Logger logger = LoggerFactory.getLogger(getClass());
void method() {
// this logging method has 2 placeholders, but only 1 parameter
logger.info("{}, {}.", new Object[]{ "Hello" });
// this is OK
logger.info("{}, {}.", new Object[]{ "Hello", "World" });
}
}
- supported from version 0.2
- this rule has a trouble, please try findbugs-slf4j instead.
- the 1st argument of logging method should be a
literal
- if the 2nd argument is array, it should be an array initializer like
new Object[]{ ... }
- first release
- added
Number of placeholder should be equal to number of argument
rule
- refactored
- downgrade XML schema to avoid warning in Eclipse (issue #1)
- added a test case and fixed a bug about exception which is out of CLASSPATH (issue #2)
- fixed wrong description for
Don't use static logger
rule - fixed NPE (issue #3)
Copyright 2012 Kengo TODA (eller86)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.