Avoid using deprecated Log4J constructors

See gh-46372

Signed-off-by: Volkan Yazıcı <volkan@yazi.ci>
This commit is contained in:
Volkan Yazıcı
2025-08-21 16:58:34 +02:00
committed by Stéphane Nicoll
parent 8cf0d17fbc
commit f7f049da1c
4 changed files with 36 additions and 14 deletions
@@ -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,25 +36,33 @@ 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 final LogEventPatternConverter delegate;
private final String separator;
@SuppressWarnings("deprecation") // https://github.com/apache/logging-log4j2/issues/3809
private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
super("WhitespaceExtendedThrowable", "throwable", options, configuration);
super("WhitespaceExtendedThrowable", "throwable");
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
this.separator = ((ExtendedThrowablePatternConverter) 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
@@ -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,22 +35,33 @@ 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 final LogEventPatternConverter delegate;
private final String separator;
@SuppressWarnings("deprecation") // https://github.com/apache/logging-log4j2/issues/3809
private WhitespaceThrowablePatternConverter(Configuration configuration, @Nullable String[] options) {
super("WhitespaceThrowable", "throwable", options, configuration);
super("WhitespaceThrowable", "throwable");
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
this.separator = ((ExtendedThrowablePatternConverter) 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
@@ -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
@@ -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