Skip to content

Commit 89f80f1

Browse files
jonasrutishauserppkarwasz
authored andcommitted
Fix o.a.l.Priority levels (#3085)
This fixes the conversions of Log4j 1 `o.a.l.Priority` from and to a Log4j2 `o.a.l.l.Level`.
1 parent e749fa1 commit 89f80f1

File tree

5 files changed

+80
-25
lines changed

5 files changed

+80
-25
lines changed

log4j-1.2-api/src/main/java/org/apache/log4j/Level.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ protected Level(
115115
final String levelStr,
116116
final int syslogEquivalent,
117117
final org.apache.logging.log4j.Level version2Equivalent) {
118-
super(level, levelStr, syslogEquivalent);
119-
this.version2Level = version2Equivalent != null ? version2Equivalent : OptionConverter.createLevel(this);
118+
super(level, levelStr, syslogEquivalent, version2Equivalent);
120119
}
121120

122121
/**
@@ -222,6 +221,7 @@ private void readObject(final ObjectInputStream s) throws IOException, ClassNotF
222221
if (levelStr == null) {
223222
levelStr = Strings.EMPTY;
224223
}
224+
version2Level = OptionConverter.createLevel(this);
225225
}
226226

227227
/**

log4j-1.2-api/src/main/java/org/apache/log4j/Priority.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.log4j;
1818

19+
import org.apache.log4j.helpers.OptionConverter;
20+
1921
/**
2022
* <em style="color:#A44">Refrain from using this class directly, use
2123
* the {@link Level} class instead.</em>
@@ -64,31 +66,31 @@ public class Priority {
6466
* @deprecated Use {@link Level#FATAL} instead.
6567
*/
6668
@Deprecated
67-
public static final Priority FATAL = new Level(FATAL_INT, "FATAL", 0);
69+
public static final Priority FATAL = new Priority(FATAL_INT, "FATAL", 0, org.apache.logging.log4j.Level.FATAL);
6870

6971
/**
7072
* @deprecated Use {@link Level#ERROR} instead.
7173
*/
7274
@Deprecated
73-
public static final Priority ERROR = new Level(ERROR_INT, "ERROR", 3);
75+
public static final Priority ERROR = new Priority(ERROR_INT, "ERROR", 3, org.apache.logging.log4j.Level.ERROR);
7476

7577
/**
7678
* @deprecated Use {@link Level#WARN} instead.
7779
*/
7880
@Deprecated
79-
public static final Priority WARN = new Level(WARN_INT, "WARN", 4);
81+
public static final Priority WARN = new Priority(WARN_INT, "WARN", 4, org.apache.logging.log4j.Level.WARN);
8082

8183
/**
8284
* @deprecated Use {@link Level#INFO} instead.
8385
*/
8486
@Deprecated
85-
public static final Priority INFO = new Level(INFO_INT, "INFO", 6);
87+
public static final Priority INFO = new Priority(INFO_INT, "INFO", 6, org.apache.logging.log4j.Level.INFO);
8688

8789
/**
8890
* @deprecated Use {@link Level#DEBUG} instead.
8991
*/
9092
@Deprecated
91-
public static final Priority DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
93+
public static final Priority DEBUG = new Priority(DEBUG_INT, "DEBUG", 7, org.apache.logging.log4j.Level.DEBUG);
9294

9395
/*
9496
* These variables should be private but were not in Log4j 1.2 so are left the same way here.
@@ -102,9 +104,7 @@ public class Priority {
102104
* Default constructor for deserialization.
103105
*/
104106
protected Priority() {
105-
level = DEBUG_INT;
106-
levelStr = "DEBUG";
107-
syslogEquivalent = 7;
107+
this(DEBUG_INT, "DEBUG", 7, org.apache.logging.log4j.Level.DEBUG);
108108
}
109109

110110
/**
@@ -114,9 +114,18 @@ protected Priority() {
114114
* @param syslogEquivalent The equivalent syslog value.
115115
*/
116116
protected Priority(final int level, final String levelStr, final int syslogEquivalent) {
117+
this(level, levelStr, syslogEquivalent, null);
118+
}
119+
120+
Priority(
121+
final int level,
122+
final String levelStr,
123+
final int syslogEquivalent,
124+
final org.apache.logging.log4j.Level version2Equivalent) {
117125
this.level = level;
118126
this.levelStr = levelStr;
119127
this.syslogEquivalent = syslogEquivalent;
128+
this.version2Level = version2Equivalent != null ? version2Equivalent : OptionConverter.createLevel(this);
120129
}
121130

122131
/**
@@ -229,7 +238,8 @@ public static Priority toPriority(final int val) {
229238
*/
230239
@Deprecated
231240
public static Priority toPriority(final int val, final Priority defaultPriority) {
232-
return Level.toLevel(val, (Level) defaultPriority);
241+
Level result = Level.toLevel(val, null);
242+
return result == null ? defaultPriority : result;
233243
}
234244

235245
/**
@@ -240,6 +250,7 @@ public static Priority toPriority(final int val, final Priority defaultPriority)
240250
*/
241251
@Deprecated
242252
public static Priority toPriority(final String sArg, final Priority defaultPriority) {
243-
return Level.toLevel(sArg, (Level) defaultPriority);
253+
Level result = Level.toLevel(sArg, null);
254+
return result == null ? defaultPriority : result;
244255
}
245256
}

log4j-1.2-api/src/test/java/org/apache/log4j/LevelTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
*/
1717
package org.apache.log4j;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

2222
import java.util.Locale;
23+
import org.apache.log4j.helpers.OptionConverter;
2324
import org.apache.log4j.util.SerializationTestHelper;
24-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.MethodSource;
2528

2629
/**
2730
* Tests of Level.
@@ -77,6 +80,7 @@ public void testCustomLevelSerialization() throws Exception {
7780
assertEquals(Level.INFO.level, clone.level);
7881
assertEquals(Level.INFO.levelStr, clone.levelStr);
7982
assertEquals(Level.INFO.syslogEquivalent, clone.syslogEquivalent);
83+
assertEquals(OptionConverter.createLevel(custom), clone.version2Level);
8084
}
8185

8286
/**
@@ -205,6 +209,15 @@ public void testALL() {
205209
assertTrue(Level.ALL instanceof Level);
206210
}
207211

212+
/**
213+
* Tests version2Level.
214+
*/
215+
@ParameterizedTest
216+
@MethodSource("org.apache.log4j.helpers.OptionConverterLevelTest#standardLevels")
217+
public void testVersion2Level(final Level log4j1Level, final org.apache.logging.log4j.Level log4j2Level) {
218+
assertEquals(log4j2Level, log4j1Level.getVersion2Level());
219+
}
220+
208221
/**
209222
* Tests Level.toLevel(Level.All_INT).
210223
*/

log4j-1.2-api/src/test/java/org/apache/log4j/PriorityTest.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
*/
1717
package org.apache.log4j;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import java.util.Locale;
24-
import org.junit.Test;
24+
import java.util.stream.Stream;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.Arguments;
28+
import org.junit.jupiter.params.provider.MethodSource;
2529

2630
/**
2731
* Tests of Priority.
@@ -85,13 +89,32 @@ public void testAllInt() {
8589
assertEquals(Integer.MIN_VALUE, Priority.ALL_INT);
8690
}
8791

92+
@SuppressWarnings("deprecation")
93+
static Stream<Arguments> testVersion2Level() {
94+
return Stream.of(
95+
Arguments.of(Priority.FATAL, org.apache.logging.log4j.Level.FATAL),
96+
Arguments.of(Priority.ERROR, org.apache.logging.log4j.Level.ERROR),
97+
Arguments.of(Priority.WARN, org.apache.logging.log4j.Level.WARN),
98+
Arguments.of(Priority.INFO, org.apache.logging.log4j.Level.INFO),
99+
Arguments.of(Priority.DEBUG, org.apache.logging.log4j.Level.DEBUG));
100+
}
101+
102+
/**
103+
* Tests version2Level.
104+
*/
105+
@ParameterizedTest
106+
@MethodSource()
107+
public void testVersion2Level(final Priority log4j1Priority, final org.apache.logging.log4j.Level log4j2Level) {
108+
assertEquals(log4j2Level, log4j1Priority.getVersion2Level());
109+
}
110+
88111
/**
89112
* Tests Priority.FATAL.
90113
*/
91114
@Test
92115
@SuppressWarnings("deprecation")
93-
public void testFatal() {
94-
assertTrue(Priority.FATAL instanceof Level);
116+
public void testFATAL() {
117+
assertFalse(Priority.FATAL instanceof Level);
95118
}
96119

97120
/**
@@ -100,7 +123,7 @@ public void testFatal() {
100123
@Test
101124
@SuppressWarnings("deprecation")
102125
public void testERROR() {
103-
assertTrue(Priority.ERROR instanceof Level);
126+
assertFalse(Priority.ERROR instanceof Level);
104127
}
105128

106129
/**
@@ -109,7 +132,7 @@ public void testERROR() {
109132
@Test
110133
@SuppressWarnings("deprecation")
111134
public void testWARN() {
112-
assertTrue(Priority.WARN instanceof Level);
135+
assertFalse(Priority.WARN instanceof Level);
113136
}
114137

115138
/**
@@ -118,7 +141,7 @@ public void testWARN() {
118141
@Test
119142
@SuppressWarnings("deprecation")
120143
public void testINFO() {
121-
assertTrue(Priority.INFO instanceof Level);
144+
assertFalse(Priority.INFO instanceof Level);
122145
}
123146

124147
/**
@@ -127,7 +150,7 @@ public void testINFO() {
127150
@Test
128151
@SuppressWarnings("deprecation")
129152
public void testDEBUG() {
130-
assertTrue(Priority.DEBUG instanceof Level);
153+
assertFalse(Priority.DEBUG instanceof Level);
131154
}
132155

133156
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3085" link="https://github.com/apache/logging-log4j2/pull/3085"/>
7+
<description format="asciidoc">Fix the conversion of `o.a.l.Priority` classes to Log4j 2 levels.</description>
8+
</entry>

0 commit comments

Comments
 (0)