Skip to content

Commit 8cf0d17

Browse files
committed
Upgrade to Log4j2 2.25.1
Closes gh-46334
1 parent 376ad32 commit 8cf0d17

File tree

13 files changed

+50
-41
lines changed

13 files changed

+50
-41
lines changed

config/checkstyle/checkstyle-suppressions.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,7 @@
7575
<suppress files="ConditionMessage\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
7676
<suppress files="EntityManagerFactoryBuilder\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
7777
<suppress files="DockerApi\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
78+
<!-- https://github.com/apache/logging-log4j2/issues/2769#issuecomment-3049020222 -->
79+
<suppress files="SpringProfileArbiter\.java" checks="SpringMethodVisibility"/>
80+
<suppress files="StructuredLogLayout\.java" checks="SpringMethodVisibility"/>
7881
</suppressions>

core/spring-boot/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ tasks.named("checkFormatMain") {
8787
source(fileTree("src/main/javaTemplates"))
8888
}
8989

90+
tasks.named("compileJava") {
91+
// Provide the project coordinates to the `GraalVmProcessor`:
92+
options.compilerArgs << '-Alog4j.graalvm.groupId=org.springframework.boot'
93+
options.compilerArgs << '-Alog4j.graalvm.artifactId=spring-boot-log4j'
94+
}
95+
9096
plugins.withType(EclipsePlugin) {
9197
eclipse {
9298
synchronizationTasks syncJavaTemplates

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ElasticCommonSchemaStructuredLogFormatter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import java.util.Objects;
2020
import java.util.Set;
2121
import java.util.TreeSet;
22+
import java.util.function.Function;
2223

2324
import org.apache.logging.log4j.Level;
2425
import org.apache.logging.log4j.Marker;
2526
import org.apache.logging.log4j.core.LogEvent;
26-
import org.apache.logging.log4j.core.impl.ThrowableProxy;
2727
import org.apache.logging.log4j.core.time.Instant;
2828
import org.apache.logging.log4j.util.ReadOnlyStringMap;
2929
import org.jspecify.annotations.Nullable;
@@ -71,10 +71,10 @@ private static void jsonMembers(Environment environment, @Nullable StackTracePri
7171
members.add("message", LogEvent::getMessage).as(StructuredMessage::get);
7272
members.from(LogEvent::getContextData)
7373
.usingPairs(contextPairs.nested(ElasticCommonSchemaStructuredLogFormatter::addContextDataPairs));
74-
members.from(LogEvent::getThrownProxy).whenNotNull().usingMembers((thrownProxyMembers) -> {
74+
members.from(LogEvent::getThrown).whenNotNull().usingMembers((thrownProxyMembers) -> {
7575
thrownProxyMembers.add("error").usingMembers((error) -> {
76-
error.add("type", ThrowableProxy::getThrowable).whenNotNull().as(ObjectUtils::nullSafeClassName);
77-
error.add("message", ThrowableProxy::getMessage);
76+
error.add("type", Function.identity()).whenNotNull().as(ObjectUtils::nullSafeClassName);
77+
error.add("message", Throwable::getMessage);
7878
error.add("stack_trace", extractor::stackTrace);
7979
});
8080
});

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ExtendedWhitespaceThrowablePatternConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public final class ExtendedWhitespaceThrowablePatternConverter extends Throwable
3939

4040
private final ExtendedThrowablePatternConverter delegate;
4141

42-
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, String @Nullable [] options) {
42+
@SuppressWarnings("deprecation") // https://github.com/apache/logging-log4j2/issues/3809
43+
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
4344
super("WhitespaceExtendedThrowable", "throwable", options, configuration);
4445
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
4546
}
@@ -61,7 +62,7 @@ public void format(LogEvent event, StringBuilder buffer) {
6162
* @return a new {@code WhitespaceThrowablePatternConverter}
6263
*/
6364
public static ExtendedWhitespaceThrowablePatternConverter newInstance(Configuration configuration,
64-
String @Nullable [] options) {
65+
@Nullable String[] options) {
6566
return new ExtendedWhitespaceThrowablePatternConverter(configuration, options);
6667
}
6768

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Extractor.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
package org.springframework.boot.logging.log4j2;
1818

19+
import java.io.PrintWriter;
20+
import java.io.StringWriter;
21+
1922
import org.apache.logging.log4j.core.LogEvent;
20-
import org.apache.logging.log4j.core.impl.ThrowableProxy;
2123
import org.jspecify.annotations.Nullable;
2224
import org.slf4j.event.LoggingEvent;
2325

2426
import org.springframework.boot.logging.StackTracePrinter;
25-
import org.springframework.util.Assert;
2627

2728
/**
2829
* Functions to extract items from {@link LoggingEvent}.
@@ -42,19 +43,23 @@ String messageAndStackTrace(LogEvent event) {
4243
}
4344

4445
@Nullable String stackTrace(LogEvent event) {
45-
return stackTrace(event.getThrownProxy());
46+
return stackTrace(event.getThrown());
4647
}
4748

48-
@Nullable String stackTrace(@Nullable ThrowableProxy throwableProxy) {
49-
if (throwableProxy == null) {
49+
@Nullable String stackTrace(@Nullable Throwable throwable) {
50+
if (throwable == null) {
5051
return null;
5152
}
5253
if (this.stackTracePrinter != null) {
53-
Throwable throwable = throwableProxy.getThrowable();
54-
Assert.state(throwable != null, "Proxy must return Throwable in order to print exception");
5554
return this.stackTracePrinter.printStackTraceToString(throwable);
5655
}
57-
return throwableProxy.getExtendedStackTraceAsString();
56+
return printStackTrace(throwable);
57+
}
58+
59+
private static String printStackTrace(Throwable throwable) {
60+
StringWriter stringWriter = new StringWriter();
61+
throwable.printStackTrace(new PrintWriter(stringWriter));
62+
return stringWriter.toString();
5863
}
5964

6065
}

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/GraylogExtendedLogFormatStructuredLogFormatter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ private static void jsonMembers(Environment environment, @Nullable StackTracePri
9898
.usingPairs(contextPairs.flat(additionalFieldJoiner(),
9999
GraylogExtendedLogFormatStructuredLogFormatter::addContextDataPairs));
100100
members.add()
101-
.whenNotNull(LogEvent::getThrownProxy)
102-
.usingMembers((thrownProxyMembers) -> throwableMembers(thrownProxyMembers, extractor));
101+
.whenNotNull(LogEvent::getThrown)
102+
.usingMembers((thrownMembers) -> throwableMembers(thrownMembers, extractor));
103103
}
104104

105105
private static String getMessageText(Message message) {
@@ -131,11 +131,9 @@ private static int convertLevel(LogEvent event) {
131131

132132
private static void throwableMembers(Members<LogEvent> members, Extractor extractor) {
133133
members.add("full_message", extractor::messageAndStackTrace);
134-
members.add("_error_type", (event) -> event.getThrownProxy().getThrowable())
135-
.whenNotNull()
136-
.as(ObjectUtils::nullSafeClassName);
134+
members.add("_error_type", LogEvent::getThrown).whenNotNull().as(ObjectUtils::nullSafeClassName);
137135
members.add("_error_stack_trace", extractor::stackTrace);
138-
members.add("_error_message", (event) -> event.getThrownProxy().getMessage());
136+
members.add("_error_message", (event) -> event.getThrown().getMessage());
139137
}
140138

141139
private static void addContextDataPairs(ContextPairs.Pairs<ReadOnlyStringMap> contextPairs) {

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/LogstashStructuredLogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static void jsonMembers(@Nullable StackTracePrinter stackTracePrinter, C
6868
.whenNotNull()
6969
.as(LogstashStructuredLogFormatter::getMarkers)
7070
.whenNot(CollectionUtils::isEmpty);
71-
members.add("stack_trace", LogEvent::getThrownProxy).whenNotNull().as(extractor::stackTrace);
71+
members.add("stack_trace", LogEvent::getThrown).whenNotNull().as(extractor::stackTrace);
7272
}
7373

7474
private static String asTimestamp(Instant instant) {

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringProfileArbiter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private Builder() {
9090
* @return this
9191
* @see Profiles#of(String...)
9292
*/
93-
Builder setName(String name) {
93+
public Builder setName(String name) {
9494
this.name = name;
9595
return this;
9696
}

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/StructuredLogLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ static final class Builder implements org.apache.logging.log4j.core.util.Builder
8787
@SuppressWarnings("NullAway.Init")
8888
private String charset = StandardCharsets.UTF_8.name();
8989

90-
Builder setFormat(String format) {
90+
public Builder setFormat(String format) {
9191
this.format = format;
9292
return this;
9393
}
9494

95-
Builder setCharset(String charset) {
95+
public Builder setCharset(String charset) {
9696
this.charset = charset;
9797
return this;
9898
}

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/WhitespaceThrowablePatternConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
@ConverterKeys({ "wEx", "wThrowable", "wException" })
3636
public final class WhitespaceThrowablePatternConverter extends ThrowablePatternConverter {
3737

38-
private WhitespaceThrowablePatternConverter(Configuration configuration, String @Nullable [] options) {
38+
@SuppressWarnings("deprecation") // https://github.com/apache/logging-log4j2/issues/3809
39+
private WhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
3940
super("WhitespaceThrowable", "throwable", options, configuration);
4041
}
4142

@@ -56,7 +57,7 @@ public void format(LogEvent event, StringBuilder buffer) {
5657
* @return a new {@code WhitespaceThrowablePatternConverter}
5758
*/
5859
public static WhitespaceThrowablePatternConverter newInstance(Configuration configuration,
59-
String @Nullable [] options) {
60+
@Nullable String[] options) {
6061
return new WhitespaceThrowablePatternConverter(configuration, options);
6162
}
6263

0 commit comments

Comments
 (0)