Upgrade to Log4j2 2.25.1

Closes gh-46334
This commit is contained in:
Stéphane Nicoll
2025-08-21 16:54:01 +02:00
parent 376ad32759
commit 8cf0d17fbc
13 changed files with 50 additions and 41 deletions
@@ -75,4 +75,7 @@
<suppress files="ConditionMessage\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="EntityManagerFactoryBuilder\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="DockerApi\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<!-- https://github.com/apache/logging-log4j2/issues/2769#issuecomment-3049020222 -->
<suppress files="SpringProfileArbiter\.java" checks="SpringMethodVisibility"/>
<suppress files="StructuredLogLayout\.java" checks="SpringMethodVisibility"/>
</suppressions>
+6
View File
@@ -87,6 +87,12 @@ tasks.named("checkFormatMain") {
source(fileTree("src/main/javaTemplates"))
}
tasks.named("compileJava") {
// Provide the project coordinates to the `GraalVmProcessor`:
options.compilerArgs << '-Alog4j.graalvm.groupId=org.springframework.boot'
options.compilerArgs << '-Alog4j.graalvm.artifactId=spring-boot-log4j'
}
plugins.withType(EclipsePlugin) {
eclipse {
synchronizationTasks syncJavaTemplates
@@ -19,11 +19,11 @@ package org.springframework.boot.logging.log4j2;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.apache.logging.log4j.core.time.Instant;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import org.jspecify.annotations.Nullable;
@@ -71,10 +71,10 @@ class ElasticCommonSchemaStructuredLogFormatter extends JsonWriterStructuredLogF
members.add("message", LogEvent::getMessage).as(StructuredMessage::get);
members.from(LogEvent::getContextData)
.usingPairs(contextPairs.nested(ElasticCommonSchemaStructuredLogFormatter::addContextDataPairs));
members.from(LogEvent::getThrownProxy).whenNotNull().usingMembers((thrownProxyMembers) -> {
members.from(LogEvent::getThrown).whenNotNull().usingMembers((thrownProxyMembers) -> {
thrownProxyMembers.add("error").usingMembers((error) -> {
error.add("type", ThrowableProxy::getThrowable).whenNotNull().as(ObjectUtils::nullSafeClassName);
error.add("message", ThrowableProxy::getMessage);
error.add("type", Function.identity()).whenNotNull().as(ObjectUtils::nullSafeClassName);
error.add("message", Throwable::getMessage);
error.add("stack_trace", extractor::stackTrace);
});
});
@@ -39,7 +39,8 @@ public final class ExtendedWhitespaceThrowablePatternConverter extends Throwable
private final ExtendedThrowablePatternConverter delegate;
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, String @Nullable [] options) {
@SuppressWarnings("deprecation") // https://github.com/apache/logging-log4j2/issues/3809
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
super("WhitespaceExtendedThrowable", "throwable", options, configuration);
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
}
@@ -61,7 +62,7 @@ public final class ExtendedWhitespaceThrowablePatternConverter extends Throwable
* @return a new {@code WhitespaceThrowablePatternConverter}
*/
public static ExtendedWhitespaceThrowablePatternConverter newInstance(Configuration configuration,
String @Nullable [] options) {
@Nullable String[] options) {
return new ExtendedWhitespaceThrowablePatternConverter(configuration, options);
}
@@ -16,13 +16,14 @@
package org.springframework.boot.logging.log4j2;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.jspecify.annotations.Nullable;
import org.slf4j.event.LoggingEvent;
import org.springframework.boot.logging.StackTracePrinter;
import org.springframework.util.Assert;
/**
* Functions to extract items from {@link LoggingEvent}.
@@ -42,19 +43,23 @@ class Extractor {
}
@Nullable String stackTrace(LogEvent event) {
return stackTrace(event.getThrownProxy());
return stackTrace(event.getThrown());
}
@Nullable String stackTrace(@Nullable ThrowableProxy throwableProxy) {
if (throwableProxy == null) {
@Nullable String stackTrace(@Nullable Throwable throwable) {
if (throwable == null) {
return null;
}
if (this.stackTracePrinter != null) {
Throwable throwable = throwableProxy.getThrowable();
Assert.state(throwable != null, "Proxy must return Throwable in order to print exception");
return this.stackTracePrinter.printStackTraceToString(throwable);
}
return throwableProxy.getExtendedStackTraceAsString();
return printStackTrace(throwable);
}
private static String printStackTrace(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}
}
@@ -98,8 +98,8 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
.usingPairs(contextPairs.flat(additionalFieldJoiner(),
GraylogExtendedLogFormatStructuredLogFormatter::addContextDataPairs));
members.add()
.whenNotNull(LogEvent::getThrownProxy)
.usingMembers((thrownProxyMembers) -> throwableMembers(thrownProxyMembers, extractor));
.whenNotNull(LogEvent::getThrown)
.usingMembers((thrownMembers) -> throwableMembers(thrownMembers, extractor));
}
private static String getMessageText(Message message) {
@@ -131,11 +131,9 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
private static void throwableMembers(Members<LogEvent> members, Extractor extractor) {
members.add("full_message", extractor::messageAndStackTrace);
members.add("_error_type", (event) -> event.getThrownProxy().getThrowable())
.whenNotNull()
.as(ObjectUtils::nullSafeClassName);
members.add("_error_type", LogEvent::getThrown).whenNotNull().as(ObjectUtils::nullSafeClassName);
members.add("_error_stack_trace", extractor::stackTrace);
members.add("_error_message", (event) -> event.getThrownProxy().getMessage());
members.add("_error_message", (event) -> event.getThrown().getMessage());
}
private static void addContextDataPairs(ContextPairs.Pairs<ReadOnlyStringMap> contextPairs) {
@@ -68,7 +68,7 @@ class LogstashStructuredLogFormatter extends JsonWriterStructuredLogFormatter<Lo
.whenNotNull()
.as(LogstashStructuredLogFormatter::getMarkers)
.whenNot(CollectionUtils::isEmpty);
members.add("stack_trace", LogEvent::getThrownProxy).whenNotNull().as(extractor::stackTrace);
members.add("stack_trace", LogEvent::getThrown).whenNotNull().as(extractor::stackTrace);
}
private static String asTimestamp(Instant instant) {
@@ -90,7 +90,7 @@ final class SpringProfileArbiter implements Arbiter {
* @return this
* @see Profiles#of(String...)
*/
Builder setName(String name) {
public Builder setName(String name) {
this.name = name;
return this;
}
@@ -87,12 +87,12 @@ final class StructuredLogLayout extends AbstractStringLayout {
@SuppressWarnings("NullAway.Init")
private String charset = StandardCharsets.UTF_8.name();
Builder setFormat(String format) {
public Builder setFormat(String format) {
this.format = format;
return this;
}
Builder setCharset(String charset) {
public Builder setCharset(String charset) {
this.charset = charset;
return this;
}
@@ -35,7 +35,8 @@ import org.jspecify.annotations.Nullable;
@ConverterKeys({ "wEx", "wThrowable", "wException" })
public final class WhitespaceThrowablePatternConverter extends ThrowablePatternConverter {
private WhitespaceThrowablePatternConverter(Configuration configuration, String @Nullable [] options) {
@SuppressWarnings("deprecation") // https://github.com/apache/logging-log4j2/issues/3809
private WhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
super("WhitespaceThrowable", "throwable", options, configuration);
}
@@ -56,7 +57,7 @@ public final class WhitespaceThrowablePatternConverter extends ThrowablePatternC
* @return a new {@code WhitespaceThrowablePatternConverter}
*/
public static WhitespaceThrowablePatternConverter newInstance(Configuration configuration,
String @Nullable [] options) {
@Nullable String[] options) {
return new WhitespaceThrowablePatternConverter(configuration, options);
}
@@ -46,7 +46,7 @@ class ExtractorTests {
}
@Test
void stackTraceWhenNoPrinterPrintsUsingLoggingSystem() {
void stackTraceWhenNoPrinterPrintsUsingFallback() {
Extractor extractor = new Extractor(null, createConverter());
assertThat(extractor.stackTrace(createEvent())).contains("java.lang.RuntimeException: Boom!");
}
+2 -11
View File
@@ -1252,23 +1252,14 @@ bom {
releaseNotes("https://github.com/liquibase/liquibase/releases/tag/v{version}")
}
}
library("Log4j2", "2.24.3") {
library("Log4j2", "2.25.1") {
prohibit {
contains "-alpha"
contains "-beta"
because "we don't want alphas or betas"
}
group("org.apache.logging.log4j") {
bom("log4j-bom") {
permit("biz.aQute.bnd:biz.aQute.bnd.annotation")
permit("com.github.spotbugs:spotbugs-annotations")
permit("org.apache.logging:logging-parent")
permit("org.apache.maven.plugin-tools:maven-plugin-annotations")
permit("org.jspecify:jspecify")
permit("org.osgi:org.osgi.annotation.bundle")
permit("org.osgi:org.osgi.annotation.versioning")
permit("org.osgi:osgi.annotation")
}
bom("log4j-bom")
}
links {
site("https://logging.apache.org/log4j")
@@ -16,8 +16,10 @@
package smoketest.structuredlogging.log4j2;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
@@ -39,9 +41,11 @@ public class CustomStructuredLogFormatter implements StructuredLogFormatter<LogE
result.append(" pid=").append(this.pid);
}
result.append(" msg=\"").append(event.getMessage().getFormattedMessage()).append('"');
ThrowableProxy throwable = event.getThrownProxy();
Throwable throwable = event.getThrown();
if (throwable != null) {
result.append(" error=\"").append(throwable.getExtendedStackTraceAsString()).append('"');
StringWriter stackTrace = new StringWriter();
throwable.printStackTrace(new PrintWriter(stackTrace));
result.append(" error=\"").append(stackTrace).append('"');
}
result.append('\n');
return result.toString();