IntelliJ: Is there an Intention Action to convert old style Log4J calls to new style SLF4J calls?

时间:2015-06-04 08:22:47

标签: java intellij-idea slf4j intellij-14

I am working on a project with legacy code that had used Log4J in the past, and now uses SLF4J. Most of the old style Log4J logging statements remain, and I convert the log statements slowly to the new SLF4J style as I come across them.

I've just realised that pressing Alt+Enter when in the old style log statements gives me the following options (unfortunately I cannot upload a screenshot):

  • Copy String concatenation text to the clipboard
  • Inject language or reference
  • Replace '+' with 'String.format()'
  • Replace '+' with 'StringBuilder.append()'
  • Replace '+' with 'java.test.MessageFormat.format()'

The option Replace '+' with 'String.format()' is very close to what I need, although I don't need the String.format( bit.

Is there something that would give me an Intention Action of: Replace Log4J style log statement with SLF4J style log statement?

As an example of old logging style (e.g. Log4J), I mean:

LOGGER.debug("User " + user.getUserId() + " has performed a search with the criteria " + dto);

And by new style I mean:

LOGGER.debug("User {} has performed a search with the criteria {}", user.getUserId(), dto);

2 个答案:

答案 0 :(得分:9)

您是否尝试过 Java |记录问题|非常量字符串连接作为记录调用检查的参数?它有一个quickfix,可以自动将字符串连接转换为参数化日志消息。

答案 1 :(得分:3)

感谢Wim Deblauwe关于SSR的评论,我发现了编辑|找到|结构替换并使用以下内容修复在日志记录语句中使用两个参数的简单情况:

搜索模板:

LOGGER.debug("$str1$" + $arg1$ + "$str2$" + $arg2$)

替换模板:

LOGGER.debug("$str1${}$str2${}", $arg1$, $arg2$)

我怀疑我是否正在使用结构搜索和替换功能来实现其最大功能,并且必须进行一些扫描以获取所有日志记录语句,但这是一个很大的进步。谢谢Wim。