Skip to content
Merged
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 @@ -98,6 +98,19 @@ void format_should_warn_on_insufficient_args(
placeholderCount, argCount, pattern);
}

@Test
void format_should_not_warn_on_insufficient_args() {
final String expectedMessage = "pan a";
final String pattern = "pan {}";
final String[] args = new String[] {"a", null};
final int argCount = args.length;

String actualMessage = ParameterFormatter.format(pattern, args, argCount);
assertThat(actualMessage).isEqualTo(expectedMessage);
final List<StatusData> statusDataList = statusListener.getStatusData().collect(Collectors.toList());
assertThat(statusDataList).isEmpty();
}

@ParameterizedTest
@MethodSource("messageFormattingTestCases")
void format_should_work(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static void formatMessage(
// #2380: check if the count of placeholder is not equal to the count of arguments
if (analysis.placeholderCount != argCount) {
final int noThrowableArgCount =
argCount < 1 ? 0 : argCount - ((args[argCount - 1] instanceof Throwable) ? 1 : 0);
argCount < 1 ? 0 : argCount - (isLastArgumentThrowable(args, argCount) ? 1 : 0);
if (analysis.placeholderCount != noThrowableArgCount) {
STATUS_LOGGER.warn(
"found {} argument placeholders, but provided {} for pattern `{}`",
Expand All @@ -268,6 +268,12 @@ static void formatMessage(
}
}

private static boolean isLastArgumentThrowable(final Object[] args, final int argCount) {
final Object lastArgument = args[argCount - 1];
// #3975: tolerate null in the last argument since it could have been Throwable parameter
return (lastArgument == null) || (lastArgument instanceof Throwable);
}

private static void formatMessageContainingNoEscapes(
final StringBuilder buffer,
final String pattern,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="changed">
<issue id="3975" link="https://github.com/apache/logging-log4j2/issues/3975"/>
<issue id="4014" link="https://github.com/apache/logging-log4j2/pull/4014"/>
<description format="asciidoc">
Prevent ParameterFormatter issuing a warning in case there is an extra null argument.
Needed to support cases with Throwable parameter that may be null.
</description>
</entry>