Re-apply the upgrade to Log4j2 2.25.1

See gh-46372
See gh-46334
This commit is contained in:
Stéphane Nicoll 2025-09-10 16:36:16 +02:00
parent 5cec09dd02
commit b7695200a9
18 changed files with 110 additions and 83 deletions

View File

@ -79,4 +79,7 @@
<suppress files="EntityManagerFactoryBuilder\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="DockerApi\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="InvocationContext\.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>

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

View File

@ -22,7 +22,6 @@ import java.util.TreeSet;
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;
@ -70,13 +69,13 @@ 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) -> {
thrownProxyMembers.add("error").usingMembers((error) -> {
error.add("type", ThrowableProxy::getThrowable).whenNotNull().as(ObjectUtils::nullSafeClassName);
error.add("message", ThrowableProxy::getMessage);
members.from(LogEvent::getThrown)
.whenNotNull()
.usingMembers((thrownMembers) -> thrownMembers.add("error").usingMembers((error) -> {
error.add("type", ObjectUtils::nullSafeClassName);
error.add("message", Throwable::getMessage);
error.add("stack_trace", extractor::stackTrace);
});
});
}));
members.add("tags", LogEvent::getMarker)
.whenNotNull()
.as(ElasticCommonSchemaStructuredLogFormatter::getMarkers)

View File

@ -21,6 +21,7 @@ import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.ExtendedThrowablePatternConverter;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.apache.logging.log4j.core.pattern.PatternConverter;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.jspecify.annotations.Nullable;
@ -35,24 +36,32 @@ import org.jspecify.annotations.Nullable;
*/
@Plugin(name = "ExtendedWhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({ "xwEx", "xwThrowable", "xwException" })
public final class ExtendedWhitespaceThrowablePatternConverter extends ThrowablePatternConverter {
public final class ExtendedWhitespaceThrowablePatternConverter extends LogEventPatternConverter {
private final ExtendedThrowablePatternConverter delegate;
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, String @Nullable [] options) {
super("WhitespaceExtendedThrowable", "throwable", options, configuration);
private final String separator;
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
super("WhitespaceExtendedThrowable", "throwable");
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
this.separator = this.delegate.getOptions().getSeparator();
}
@Override
public void format(LogEvent event, StringBuilder buffer) {
if (event.getThrown() != null) {
buffer.append(this.options.getSeparator());
buffer.append(this.separator);
this.delegate.format(event, buffer);
buffer.append(this.options.getSeparator());
buffer.append(this.separator);
}
}
@Override
public boolean handlesThrowable() {
return true;
}
/**
* Creates a new instance of the class. Required by Log4J2.
* @param configuration current configuration
@ -61,7 +70,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);
}

View File

@ -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();
}
}

View File

@ -98,11 +98,11 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
.whenNot(mapIsEmpty)
.usingPairs(contextPairs.flat(additionalFieldJoiner(),
GraylogExtendedLogFormatStructuredLogFormatter::addContextDataPairs));
Function<@Nullable LogEvent, @Nullable Object> getThrownProxy = (event) -> (event != null)
? event.getThrownProxy() : null;
Function<@Nullable LogEvent, @Nullable Object> getThrown = (event) -> (event != null) ? event.getThrown()
: null;
members.add()
.whenNotNull(getThrownProxy)
.usingMembers((thrownProxyMembers) -> throwableMembers(thrownProxyMembers, extractor));
.whenNotNull(getThrown)
.usingMembers((thrownMembers) -> throwableMembers(thrownMembers, extractor));
}
private static String getMessageText(Message message) {
@ -134,11 +134,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) {

View File

@ -71,7 +71,7 @@ class LogstashStructuredLogFormatter extends JsonWriterStructuredLogFormatter<Lo
.whenNotNull()
.as(LogstashStructuredLogFormatter::getMarkers)
.whenNot(collectionIsEmpty);
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) {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -20,6 +20,8 @@ import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.ExtendedThrowablePatternConverter;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.apache.logging.log4j.core.pattern.PatternConverter;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.jspecify.annotations.Nullable;
@ -33,21 +35,32 @@ import org.jspecify.annotations.Nullable;
*/
@Plugin(name = "WhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({ "wEx", "wThrowable", "wException" })
public final class WhitespaceThrowablePatternConverter extends ThrowablePatternConverter {
public final class WhitespaceThrowablePatternConverter extends LogEventPatternConverter {
private WhitespaceThrowablePatternConverter(Configuration configuration, String @Nullable [] options) {
super("WhitespaceThrowable", "throwable", options, configuration);
private final ExtendedThrowablePatternConverter delegate;
private final String separator;
private WhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
super("WhitespaceThrowable", "throwable");
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
this.separator = this.delegate.getOptions().getSeparator();
}
@Override
public void format(LogEvent event, StringBuilder buffer) {
if (event.getThrown() != null) {
buffer.append(this.options.getSeparator());
super.format(event, buffer);
buffer.append(this.options.getSeparator());
buffer.append(this.separator);
this.delegate.format(event, buffer);
buffer.append(this.separator);
}
}
@Override
public boolean handlesThrowable() {
return true;
}
/**
* Creates a new instance of the class. Required by Log4J2.
* @param configuration current configuration
@ -56,7 +69,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);
}

View File

@ -101,13 +101,14 @@ class ElasticCommonSchemaStructuredLogFormatterTests extends AbstractStructuredL
expectedError.put("message", "Boom");
assertThat(error).containsAllEntriesOf(expectedError);
String stackTrace = (String) error.get("stack_trace");
assertThat(stackTrace).startsWith(
"""
java.lang.RuntimeException: Boom
\tat org.springframework.boot.logging.log4j2.ElasticCommonSchemaStructuredLogFormatterTests.shouldFormatException""");
assertThat(json).contains(
"""
java.lang.RuntimeException: Boom\\n\\tat org.springframework.boot.logging.log4j2.ElasticCommonSchemaStructuredLogFormatterTests.shouldFormatException""");
assertThat(stackTrace)
.startsWith(String.format("java.lang.RuntimeException: Boom%n\tat org.springframework.boot.logging.log4j2."
+ "ElasticCommonSchemaStructuredLogFormatterTests.shouldFormatException"));
assertThat(json).contains(String
.format("java.lang.RuntimeException: Boom%n\\tat org.springframework.boot.logging.log4j2."
+ "ElasticCommonSchemaStructuredLogFormatterTests.shouldFormatException")
.replace("\n", "\\n")
.replace("\r", "\\r"));
}
@Test

View File

@ -19,7 +19,7 @@ package org.springframework.boot.logging.log4j2;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class ExtendedWhitespaceThrowablePatternConverterTests {
private final ThrowablePatternConverter converter = ExtendedWhitespaceThrowablePatternConverter
private final LogEventPatternConverter converter = ExtendedWhitespaceThrowablePatternConverter
.newInstance(new DefaultConfiguration(), new String[] {});
@Test

View File

@ -136,21 +136,18 @@ class GraylogExtendedLogFormatStructuredLogFormatterTests extends AbstractStruct
String fullMessage = (String) deserialized.get("full_message");
String stackTrace = (String) deserialized.get("_error_stack_trace");
assertThat(fullMessage).startsWith(
"""
message
java.lang.RuntimeException: Boom
\tat org.springframework.boot.logging.log4j2.GraylogExtendedLogFormatStructuredLogFormatterTests.shouldFormatException""");
assertThat(stackTrace).startsWith(
"""
java.lang.RuntimeException: Boom
\tat org.springframework.boot.logging.log4j2.GraylogExtendedLogFormatStructuredLogFormatterTests.shouldFormatException""");
"message\n\njava.lang.RuntimeException: Boom%n\tat org.springframework.boot.logging.log4j2.GraylogExtendedLogFormatStructuredLogFormatterTests.shouldFormatException"
.formatted());
assertThat(deserialized)
.containsAllEntriesOf(map("_error_type", "java.lang.RuntimeException", "_error_message", "Boom"));
assertThat(stackTrace).startsWith(
"java.lang.RuntimeException: Boom%n\tat org.springframework.boot.logging.log4j2.GraylogExtendedLogFormatStructuredLogFormatterTests.shouldFormatException"
.formatted());
assertThat(json).contains(
"""
message\\n\\njava.lang.RuntimeException: Boom\\n\\tat org.springframework.boot.logging.log4j2.GraylogExtendedLogFormatStructuredLogFormatterTests.shouldFormatException""");
"java.lang.RuntimeException: Boom%n\\tat org.springframework.boot.logging.log4j2.GraylogExtendedLogFormatStructuredLogFormatterTests.shouldFormatException"
.formatted()
.replace("\n", "\\n")
.replace("\r", "\\r"));
}
@Test

View File

@ -80,12 +80,13 @@ class LogstashStructuredLogFormatterTests extends AbstractStructuredLoggingTests
Map<String, Object> deserialized = deserialize(json);
String stackTrace = (String) deserialized.get("stack_trace");
assertThat(stackTrace).startsWith(
"""
java.lang.RuntimeException: Boom
\tat org.springframework.boot.logging.log4j2.LogstashStructuredLogFormatterTests.shouldFormatException""");
"java.lang.RuntimeException: Boom%n\tat org.springframework.boot.logging.log4j2.LogstashStructuredLogFormatterTests.shouldFormatException"
.formatted());
assertThat(json).contains(
"""
java.lang.RuntimeException: Boom\\n\\tat org.springframework.boot.logging.log4j2.LogstashStructuredLogFormatterTests.shouldFormatException""");
"java.lang.RuntimeException: Boom%n\\tat org.springframework.boot.logging.log4j2.LogstashStructuredLogFormatterTests.shouldFormatException"
.formatted()
.replace("\n", "\\n")
.replace("\r", "\\r"));
}
@Test

View File

@ -19,7 +19,7 @@ package org.springframework.boot.logging.log4j2;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class WhitespaceThrowablePatternConverterTests {
private final ThrowablePatternConverter converter = WhitespaceThrowablePatternConverter
private final LogEventPatternConverter converter = WhitespaceThrowablePatternConverter
.newInstance(new DefaultConfiguration(), new String[] {});
@Test

View File

@ -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!");
}

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")

View File

@ -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();