Skip to content

Commit 1aa3952

Browse files
authored
fix: any last argument matching Throwable.toString() is left as is (#229)c
In SLF4J, the last argument of the logging methods will be treated specially and removed from the formatted string argument array if it's a Throwable. When the last argument was a `.toString()` call we stripped it even if it was a `Throwable` which changed the behaviour. https://www.slf4j.org/faq.html#paramException
1 parent 6d64cce commit 1aa3952

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/main/java/org/openrewrite/java/logging/slf4j/StripToStringFromArguments.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation m, ExecutionC
7878
J.MethodInvocation toStringInvocation = (J.MethodInvocation) arg;
7979
if (TO_STRING_MATCHER.matches(toStringInvocation.getMethodType()) &&
8080
toStringInvocation.getSelect() != null &&
81-
!(firstFormatArgIndex == lastArgIndex && TypeUtils.isAssignableTo("java.lang.Throwable", toStringInvocation.getSelect().getType()))) {
81+
!(index == lastArgIndex && TypeUtils.isAssignableTo("java.lang.Throwable", toStringInvocation.getSelect().getType()))) {
8282
// Strip the `.toString()` call
8383
return toStringInvocation.getSelect().withPrefix(toStringInvocation.getPrefix());
8484
}

src/test/java/org/openrewrite/java/logging/slf4j/StripToStringFromArgumentsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ record TestCase(String originalArgs, String expectedArgs) {
5757
", exception, o1"),
5858
new TestCase(
5959
", o1, exception.toString()",
60-
", o1, exception")
60+
", o1, exception.toString()")
6161
);
6262

6363
return Stream.of("trace", "debug", "info", "warn", "error")

0 commit comments

Comments
 (0)