Skip to content

Commit 0e7fda3

Browse files
committed
MIR-1436 replace PMD rule ProperLogger with custom PMD rules
1 parent a701d19 commit 0e7fda3

File tree

11 files changed

+215
-17
lines changed

11 files changed

+215
-17
lines changed

mir-module/src/main/java/org/mycore/mir/MIRGetOpenAIREProjectsServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MIRGetOpenAIREProjectsServlet extends HttpServlet {
2424
@Serial
2525
private static final long serialVersionUID = 1L;
2626

27-
private static Logger LOGGER = LogManager.getLogger();
27+
private static final Logger LOGGER = LogManager.getLogger();
2828

2929
private HttpClient client;
3030

mir-module/src/main/java/org/mycore/mir/authorization/accesskeys/MIRAccessKeyEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
public class MIRAccessKeyEventHandler extends MCREventHandlerBase {
4040

41-
private static Logger LOGGER = LogManager.getLogger();
41+
private static final Logger LOGGER = LogManager.getLogger();
4242

4343
/* (non-Javadoc)
4444
* @see org.mycore.common.events.MCREventHandlerBase#handleObjectCreated(org.mycore.common.events.MCREvent, org.mycore.datamodel.metadata.MCRObject)

mir-module/src/main/java/org/mycore/mir/migration/MIRMigration202006Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
name = "MIR migration 2020.06")
5252
public class MIRMigration202006Utils {
5353

54-
public static final Logger LOGGER = LogManager.getLogger();
54+
private static final Logger LOGGER = LogManager.getLogger();
5555

5656
private static final MCRCategoryID CONTENT_TYPE_ID = MCRCategoryID.fromString("derivate_types:content");
5757

mir-module/src/main/java/org/mycore/mir/migration/MIRMigration202106Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
name = "MIR migration 2021.06")
5151
public class MIRMigration202106Utils {
5252

53-
public static final Logger LOGGER = LogManager.getLogger();
53+
private static final Logger LOGGER = LogManager.getLogger();
5454

5555
protected static final String ACCESS_KEY_PREFIX = "acckey_";
5656

mir-module/src/main/java/org/mycore/mir/sword2/MIRGoobiIngester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
public class MIRGoobiIngester extends MIRSwordIngesterBase {
3131

32-
public static final Logger LOGGER = LogManager.getLogger();
32+
private static final Logger LOGGER = LogManager.getLogger();
3333

3434
@Override
3535
public MCRObjectID ingestMetadata(Deposit entry) throws SwordError, SwordServerException {

mir-module/src/main/java/org/mycore/mir/sword2/MIRMETSIngester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
public class MIRMETSIngester extends MIRSwordIngesterBase {
4444

45-
public static final Logger LOGGER = LogManager.getLogger();
45+
private static final Logger LOGGER = LogManager.getLogger();
4646

4747
private static Document readXMLFile(Path file) {
4848
try (InputStream metsIS = Files.newInputStream(file)) {

mir-wizard/src/main/java/org/mycore/mir/wizard/MIRWizardCommandResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.logging.log4j.core.layout.PatternLayout;
4242
import org.jdom2.Element;
4343

44+
@SuppressWarnings({"PMD.MCR.Logger.MatchingFieldName"})
4445
public class MIRWizardCommandResult {
4546

4647
private final static Logger ROOT_LOGGER = (Logger) LogManager.getRootLogger();

mir-wizard/src/main/java/org/mycore/mir/wizard/command/MIRWizardDownloadDBLib.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class MIRWizardDownloadDBLib extends MIRWizardCommand {
3838

39-
private static Logger LOGGER = LogManager.getLogger();
39+
private static final Logger LOGGER = LogManager.getLogger();
4040

4141
public MIRWizardDownloadDBLib() {
4242
this("download");

mir-wizard/src/main/java/org/mycore/mir/wizard/utils/MIRWizardUnzip.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
*
4444
*/
4545
public class MIRWizardUnzip {
46-
private static final Logger LOGGER = LogManager.getLogger(MIRWizardUnzip.class);
46+
47+
private static final Logger LOGGER = LogManager.getLogger();
4748

4849
/**
4950
* Size of the buffer to read/write data.

rules.xml

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Custom MyCoRe rules" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
5+
6+
<description>
7+
This ruleset contains custom MyCoRe rules
8+
</description>
9+
10+
<rule name="MCR.Logger.MaxOneField"
11+
language="java"
12+
message="There should only be one field with name 'logger' or 'LOGGER' and type 'Logger'"
13+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
14+
<priority>4</priority>
15+
<properties>
16+
<property name="xpath">
17+
<value>
18+
//ClassBody/FieldDeclaration
19+
[
20+
VariableDeclarator/VariableId/@Name=('logger','LOGGER')
21+
or
22+
ClassType[@SimpleName='Logger']
23+
]
24+
[
25+
position()>1
26+
]
27+
</value>
28+
</property>
29+
</properties>
30+
</rule>
31+
32+
<rule name="MCR.Logger.MatchingLoggerType"
33+
language="java"
34+
message="A field with name 'logger' or 'LOGGER' should have type 'Logger'"
35+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
36+
<priority>4</priority>
37+
<properties>
38+
<property name="xpath">
39+
<value>
40+
//ClassBody/FieldDeclaration
41+
[
42+
VariableDeclarator/VariableId/@Name=('logger','LOGGER')
43+
and
44+
not(ClassType[@SimpleName='Logger'])
45+
]
46+
</value>
47+
</property>
48+
</properties>
49+
</rule>
50+
51+
<rule name="MCR.Logger.MatchingFieldName"
52+
language="java"
53+
message="A field with type 'Logger' should have name 'logger' or 'LOGGER'"
54+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
55+
<priority>4</priority>
56+
<properties>
57+
<property name="xpath">
58+
<value>
59+
//ClassBody/FieldDeclaration
60+
[
61+
ClassType[@SimpleName='Logger']
62+
and
63+
not(VariableDeclarator/VariableId/@Name=('logger','LOGGER'))
64+
]
65+
</value>
66+
</property>
67+
</properties>
68+
</rule>
69+
70+
<rule name="MCR.Logger.CorrectLoggerType"
71+
language="java"
72+
message="A field with type 'Logger' should have name actual type 'org.apache.logging.log4j.Logger'"
73+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
74+
<priority>4</priority>
75+
<properties>
76+
<property name="xpath">
77+
<value>
78+
//ClassBody/FieldDeclaration
79+
[
80+
ClassType[@SimpleName='Logger']
81+
and
82+
not(ClassType[pmd-java:typeIs('org.apache.logging.log4j.Logger')])
83+
]
84+
</value>
85+
</property>
86+
</properties>
87+
</rule>
88+
89+
<rule name="MCR.Logger.CorrectStaticInitialisation"
90+
language="java"
91+
message="A field with type 'Logger' and name 'LOGGER' should be initialized as 'private static final Logger LOGGER = LogManager.getLogger();'"
92+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
93+
<priority>4</priority>
94+
<properties>
95+
<property name="xpath">
96+
<value>
97+
//ClassBody/FieldDeclaration
98+
[
99+
ClassType[@SimpleName='Logger']
100+
and
101+
VariableDeclarator/VariableId/@Name='LOGGER'
102+
]
103+
[
104+
not(
105+
deep-equal(pmd-java:modifiers(), ('private','static','final'))
106+
and
107+
VariableDeclarator/MethodCall[
108+
TypeExpression/ClassType[@SimpleName='LogManager']
109+
and
110+
@MethodName='getLogger'
111+
and
112+
count(*)=2
113+
and
114+
count(ArgumentList/*)=0
115+
]
116+
)
117+
]
118+
</value>
119+
</property>
120+
</properties>
121+
</rule>
122+
123+
<rule name="MCR.Logger.CorrectDynamicInitialisation"
124+
language="java"
125+
message="A field with type 'Logger' and name 'logger' should be initialized as 'protected final Logger LOGGER = LogManager.getLogger(getClass());'"
126+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
127+
<priority>4</priority>
128+
<properties>
129+
<property name="xpath">
130+
<value>
131+
//ClassBody/FieldDeclaration
132+
[
133+
ClassType[@SimpleName='Logger']
134+
and
135+
VariableDeclarator/VariableId/@Name='logger'
136+
]
137+
[
138+
not(
139+
deep-equal(pmd-java:modifiers(), ('protected','final'))
140+
and
141+
VariableDeclarator/MethodCall[
142+
TypeExpression/ClassType[@SimpleName='LogManager']
143+
and
144+
@MethodName='getLogger'
145+
and
146+
count(*)=2
147+
and
148+
count(ArgumentList/*)=1
149+
and
150+
ArgumentList[1][
151+
MethodCall[
152+
@MethodName='getClass'
153+
and
154+
count(*)=1
155+
and
156+
count(ArgumentList/*)=0
157+
]
158+
]
159+
]
160+
)
161+
]
162+
</value>
163+
</property>
164+
</properties>
165+
</rule>
166+
167+
<rule name="MCR.Logger.IndirectAccess"
168+
language="java"
169+
message="A field with name 'Logger' should be accessed directly (either logger.info(...) or this.logger.info(...))"
170+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
171+
<priority>4</priority>
172+
<properties>
173+
<property name="xpath">
174+
<value>
175+
//FieldAccess[@Name='logger'][VariableAccess|ConstructorCall]
176+
</value>
177+
</property>
178+
</properties>
179+
</rule>
180+
181+
<!--rule name="MCR.Logger.AbstractClassesShouldHaveDynamic"
182+
language="java"
183+
message="An abstract class should have a Logger initialized as 'protected final Logger LOGGER = LogManager.getLogger(getClass());' "
184+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
185+
<priority>5</priority>
186+
<properties>
187+
<property name="xpath">
188+
<value>
189+
//ClassBody/FieldDeclaration
190+
[
191+
../..[pmd-java:modifiers()='abstract']
192+
and
193+
ClassType[@SimpleName='Logger']
194+
and
195+
VariableDeclarator/VariableId/@Name!='logger'
196+
]
197+
</value>
198+
</property>
199+
</properties>
200+
</rule-->
201+
202+
</ruleset>

0 commit comments

Comments
 (0)