Skip to content

Commit f255f0b

Browse files
committed
Merge remote-tracking branch 'origin/2.x' into feature/2.x/cached-instant-formatter
2 parents b3a6fcc + e8d5ffc commit f255f0b

File tree

25 files changed

+337
-177
lines changed

25 files changed

+337
-177
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
/**

log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.io.Serializable;
2423
import java.lang.ref.WeakReference;
2524
import java.net.URI;
2625
import java.util.ArrayList;
@@ -40,7 +39,6 @@
4039
import org.apache.logging.log4j.Level;
4140
import org.apache.logging.log4j.core.Appender;
4241
import org.apache.logging.log4j.core.Filter;
43-
import org.apache.logging.log4j.core.Layout;
4442
import org.apache.logging.log4j.core.LifeCycle2;
4543
import org.apache.logging.log4j.core.LogEvent;
4644
import org.apache.logging.log4j.core.LoggerContext;
@@ -58,7 +56,6 @@
5856
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
5957
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
6058
import org.apache.logging.log4j.core.filter.AbstractFilterable;
61-
import org.apache.logging.log4j.core.layout.PatternLayout;
6259
import org.apache.logging.log4j.core.lookup.ConfigurationStrSubstitutor;
6360
import org.apache.logging.log4j.core.lookup.Interpolator;
6461
import org.apache.logging.log4j.core.lookup.PropertiesLookup;
@@ -779,11 +776,7 @@ public static Level getDefaultLevel() {
779776
protected void setToDefault() {
780777
// LOG4J2-1176 facilitate memory leak investigation
781778
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
782-
final Layout<? extends Serializable> layout = PatternLayout.newBuilder()
783-
.withPattern(DefaultConfiguration.DEFAULT_PATTERN)
784-
.withConfiguration(this)
785-
.build();
786-
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(layout);
779+
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(DefaultLayout.INSTANCE);
787780
appender.start();
788781
addAppender(appender);
789782
final LoggerConfig rootLoggerConfig = getRootLogger();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.config;
18+
19+
import java.nio.charset.Charset;
20+
import java.util.Collections;
21+
import java.util.Map;
22+
import org.apache.logging.log4j.core.LogEvent;
23+
import org.apache.logging.log4j.core.StringLayout;
24+
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
25+
import org.apache.logging.log4j.status.StatusData;
26+
27+
/**
28+
* A simple layout used only by {@link DefaultConfiguration}
29+
* <p>
30+
* This layout allows to create applications that don't contain {@link org.apache.logging.log4j.core.layout.PatternLayout}
31+
* and all its patterns, e.g. GraalVM applications.
32+
* </p>
33+
*
34+
* @since 2.25.0
35+
*/
36+
final class DefaultLayout implements StringLayout {
37+
38+
static final StringLayout INSTANCE = new DefaultLayout();
39+
40+
private DefaultLayout() {}
41+
42+
@Override
43+
public String toSerializable(LogEvent event) {
44+
return new StatusData(
45+
event.getSource(),
46+
event.getLevel(),
47+
event.getMessage(),
48+
event.getThrown(),
49+
event.getThreadName())
50+
.getFormattedStatus();
51+
}
52+
53+
@Override
54+
public byte[] toByteArray(LogEvent event) {
55+
return toSerializable(event).getBytes(Charset.defaultCharset());
56+
}
57+
58+
@Override
59+
public void encode(LogEvent event, ByteBufferDestination destination) {
60+
final byte[] data = toByteArray(event);
61+
destination.writeBytes(data, 0, data.length);
62+
}
63+
64+
@Override
65+
public String getContentType() {
66+
return "text/plain";
67+
}
68+
69+
@Override
70+
public Charset getCharset() {
71+
return Charset.defaultCharset();
72+
}
73+
74+
@Override
75+
public byte[] getFooter() {
76+
return null;
77+
}
78+
79+
@Override
80+
public byte[] getHeader() {
81+
return null;
82+
}
83+
84+
@Override
85+
public Map<String, String> getContentFormat() {
86+
return Collections.emptyMap();
87+
}
88+
}

log4j-parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
<jmdns.version>3.5.12</jmdns.version>
109109
<jmh.version>1.37</jmh.version>
110110
<junit.version>4.13.2</junit.version>
111-
<junit-jupiter.version>5.11.2</junit-jupiter.version>
111+
<junit-jupiter.version>5.11.3</junit-jupiter.version>
112112
<junit-pioneer.version>1.9.1</junit-pioneer.version>
113113
<kafka.version>3.8.0</kafka.version>
114114
<lightcouch.version>0.2.0</lightcouch.version>

0 commit comments

Comments
 (0)