Structural replace for log statements

Answered

I am using a logging library which supports logging varargs of key value pairs e.g.

log.info("a_string", "another_string", "key1", value1, "key2", value2, "key3", value3);

I am trying to use SSR to convert these log statements to the log statement supported by slf4j. So I want to convert above statement to,

log.info("a_string another_string key1={} key2={} key3={}", value1, value2, value3);

Since my logging library supports passing varargs of key-value pairs, the only way I found was to have multiple SSR templates to support the slf4j migration i.e. one SSR template each supporting 1 key-value pair, 2 key-value pairs, 3 key-value pair and so on. Is there a way to write more generic SSR template to detect and convert variable number of key-value pairs into the format expected by slf4j?

Thanks,

Amol

2
1 comment
Avatar
Permanently deleted user

I found the solution to this. Its hacky, but works.

Search template-

`log.info($name$, $txt$, $nameorvalue$)`
where name and txt matches exactly once, nameorvalue matches anywhere between 1 to infinity

Replace template-

`log.info($format$, $variables$)`
- Click edit variables
- Select format and click on script text. Paste following code,
StringBuilder sb = new StringBuilder();
sb.append("\"");
if (name instanceof com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl) {
sb.append("{}");
} else {
sb.append(name.getValue());
}
sb.append(" ");
if (txt instanceof com.intellij.psi.PsiLiteralExpression) {
sb.append(txt.getValue());
} else {
sb.append("{}");
}
sb.append(" ");
if (nameorvalue instanceof com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl) {
sb.append("{} ");
} else if (nameorvalue instanceof com.intellij.psi.PsiLiteralExpression) {
sb.append(nameorvalue.getValue());
} else if (nameorvalue instanceof com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl) {
sb.append("{}");
} else {
for (int i = 0; i < nameorvalue.size(); i++) {
if (i%2 == 1 && nameorvalue.get(i) instanceof com.intellij.psi.PsiLiteralExpression) {
sb.append(nameorvalue.get(i).getValue());
} else {
if (nameorvalue.get(i) instanceof com.intellij.psi.PsiLiteralExpression) {
sb.append(nameorvalue.get(i).getValue());
} else {
sb.append("{}")
}
}
sb.append(" ");
}
}
sb.append("\"");
sb.toString()
- Click on variables and enter following in the script text

StringBuilder sb = new StringBuilder();
if (name instanceof com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl) {
sb.append(name.getText());
sb.append(", ");
}

if (!(txt instanceof com.intellij.psi.PsiLiteralExpression)) {
sb.append(txt.getText());
}

if (nameorvalue instanceof com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl) {
sb.append(nameorvalue.getCanonicalText());
} else if (nameorvalue instanceof com.intellij.psi.PsiLiteralExpression) {

} else if (nameorvalue instanceof com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl) {
sb.append(nameorvalue.getText());
} else {
// sb.append(", ");
for (int i = 0; i < nameorvalue.size(); i++) {
if (!(nameorvalue.get(i) instanceof com.intellij.psi.PsiLiteralExpression)) {
sb.append(nameorvalue.get(i).getText());
if (i != nameorvalue.size() - 1) {
sb.append(", ");
}
}
}
}
sb.toString()
0

Please sign in to leave a comment.