Repository navigation
Expand file tree
/
Copy pathlogging-java.xml
More file actions
179 lines (167 loc) · 4.88 KB
/
Copy pathlogging-java.xml
File metadata and controls
179 lines (167 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?xml version="1.0"?>
<ruleset name="Java Logging Rules" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../etc/ruleset_xml_schema.xml">
<description>
The Java Logging ruleset contains a collection of rules that find questionable usages of the Logger.
</description>
<rule name="MoreThanOneLogger"
message="Class contains more than one Logger"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/logging-java.html#MoreThanOneLogger">
<description>
Normally only one logger is used in each class.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceBody [
count(
//VariableDeclarator[../Type/ReferenceType/ClassOrInterfaceType[@Image='Logger']]
)>1
] ]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
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());
}
]]>
</example>
</rule>
<rule name="LoggerIsNotStaticFinal"
message="The Logger variable declaration does not contain the static and final modifiers"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/logging-java.html#LoggerIsNotStaticFinal">
<description>
In most cases, the Logger can be declared static and final.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//VariableDeclarator
[../Type/ReferenceType
/ClassOrInterfaceType[@Image='Logger']
and
(..[@Final='false'] or ..[@Static = 'false'] ) ]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
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());
}
]]>
</example>
</rule>
<!-- This rule will probably be implemented in a java class, since we need to search the logger name
first -->
<!--
<rule name="LogBlockWithoutIf"
message="There is log block not surrounded by if"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
When many log statements are used, it is conveninent to surround them with an if.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
// Add this for performance
// if (log.isLoggable(Level.FINE)) { ...
log.fine("This happens");
log.fine("Value of x:"+x);
log.fine("Value of y:"+y);
log.fine("Value of z:"+z);
log.fine("Value of w:"+w);
]]>
</example>
</rule>
-->
<rule name="SystemPrintln"
message="System.out.print is used"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/logging-java.html#SystemPrintln">
<description>
System.(out|err).print is used, consider using a logger.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
class Foo{
Logger log = Logger.getLogger(Foo.class.getName());
public void testA () {
System.out.println("Entering test");
// Better use this
log.fine("Entering test");
}
}
]]>
</example>
</rule>
<rule name="AvoidPrintStackTrace"
message="Avoid printStackTrace(); use a logger call instead."
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/logging-java.html#AvoidPrintStackTrace">
<description>
Avoid printStackTrace(); use a logger call instead.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name[contains(@Image,'printStackTrace')]]
[PrimarySuffix[not(boolean(Arguments/ArgumentList/Expression))]]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
class Foo {
void bar() {
try {
// do something
} catch (Exception e) {
e.printStackTrace();
}
}
}
]]>
</example>
</rule>
</ruleset>