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 @@ -57,6 +57,7 @@ public String getDisplayName() {

@Override
public String getDescription() {
//language=markdown
return "Transform logging statements using concatenation for messages and variables into a parameterized format. " +
"For example, `logger.info(\"hi \" + userName)` becomes `logger.info(\"hi {}\", userName)`. This can " +
"significantly boost performance for messages that otherwise would be assembled with String concatenation. " +
Expand Down Expand Up @@ -119,7 +120,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
MessageAndArguments literalAndArgs = concatenationToLiteral(message, new MessageAndArguments("", new ArrayList<>()));
messageBuilder.append(literalAndArgs.message);
messageBuilder.append("\"");
literalAndArgs.arguments.forEach(arg -> messageBuilder.append(", #{any()}"));
// Cast Throwables to Object to preserve toString() behavior
literalAndArgs.arguments.forEach(arg -> messageBuilder.append(
TypeUtils.isAssignableTo("java.lang.Throwable", arg.getType()) ?
", (Object) #{any()}" :
", #{any()}"));
} else {
messageBuilder.append("#{any()}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ static void method(Logger logger, String text) {
);
}

@Issue("https://github.com/openrewrite/rewrite-logging-frameworks/issues/257")
@Test
void exceptionArgumentsAsConcatenatedString() {
rewriteRun(
Expand Down Expand Up @@ -250,7 +251,7 @@ static void asInteger(Logger logger, String numberString) {
try {
Integer i = Integer.valueOf(numberString);
} catch (NumberFormatException ex) {
logger.debug("some big error: {}", ex);
logger.debug("some big error: {}", (Object) ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void test() {
try {
throw new IllegalStateException("oops");
} catch (Exception e) {
logger.error("aaa: {}", e);
logger.error("aaa: {}", (Object) e);
logger.error("bbb: {}", String.valueOf(e));
logger.error("ccc: {}", e.toString());
}
Expand Down