Skip to content

Avoid using deprecated Log4j exception converter ctors #46372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;

Expand All @@ -34,24 +35,32 @@
*/
@Plugin(name = "ExtendedWhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({ "xwEx", "xwThrowable", "xwException" })
public final class ExtendedWhitespaceThrowablePatternConverter extends ThrowablePatternConverter {
public final class ExtendedWhitespaceThrowablePatternConverter extends LogEventPatternConverter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick: Since ExtendedWhitespaceThrowablePatternConverter and WhitespaceThrowablePatternConverter differ only in the specific LogEventPatternConverter they delegate to, the implementation can be simplified by:

  • Having one implemented in terms of the other, or
  • Taking advantage of the fact that pattern converters don’t need to directly implement LogEventPatternConverter, but only provide a public static LogEventPatternConverter newInstance() method, you could have both plugins return the same shared private implementation of LogEventPatternConverter, reducing duplication.


private final ExtendedThrowablePatternConverter delegate;
private final LogEventPatternConverter delegate;

private final String separator;

private ExtendedWhitespaceThrowablePatternConverter(Configuration configuration, String[] options) {
super("WhitespaceExtendedThrowable", "throwable", options, configuration);
super("WhitespaceExtendedThrowable", "throwable");
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
this.separator = WhitespaceThrowablePatternConverter.readSeparatorOption(options);
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
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;

Expand All @@ -32,21 +34,43 @@
*/
@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;

private WhitespaceThrowablePatternConverter(Configuration configuration, String[] options) {
super("WhitespaceThrowable", "throwable", options, configuration);
super("WhitespaceThrowable", "throwable");
this.delegate = ExtendedThrowablePatternConverter.newInstance(configuration, options);
this.separator = readSeparatorOption(options);
}

static String readSeparatorOption(String[] options) {
if (options != null) {
for (String option : options) {
if (option != null && option.startsWith("separator(") && option.endsWith(")")) {
return option.substring("separator(".length(), option.length() - 1);
}
}
}
return System.lineSeparator();
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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;
Expand All @@ -32,7 +32,7 @@
*/
class ExtendedWhitespaceThrowablePatternConverterTests {

private final ThrowablePatternConverter converter = ExtendedWhitespaceThrowablePatternConverter
private final LogEventPatternConverter converter = ExtendedWhitespaceThrowablePatternConverter
.newInstance(new DefaultConfiguration(), new String[] {});

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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;
Expand All @@ -31,7 +31,7 @@
*/
class WhitespaceThrowablePatternConverterTests {

private final ThrowablePatternConverter converter = WhitespaceThrowablePatternConverter
private final LogEventPatternConverter converter = WhitespaceThrowablePatternConverter
.newInstance(new DefaultConfiguration(), new String[] {});

@Test
Expand Down