Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 385
vegemite4me Avatar asked Jun 04 '15 08:06

vegemite4me


People also ask

Is IntelliJ affected by Log4j?

As we have previously announced, the IDEs based on the IntelliJ Platform are not affected by this vulnerability, because they use a patched version of log4j 1.2 with all network-related code removed.

Why should we use SLF4J?

This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J.

Does SLF4J use Log4j?

SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP.

How do I disable SLF4J?

To disable this behavior, you must add a logback configuration file called logback. xml in your java classpath root. You can download this minimal logback. xml file and add it in the src/main/resources directory for a maven project or beside fr directory for a simple java project.


1 Answers

Have you tried the Java | Logging issues | Non-constant string concatenation as argument to logging call inspection? It has a quickfix to automatically convert a string concatenation to a parameterized log message.

like image 153
Bas Leijdekkers Avatar answered Oct 20 '22 17:10

Bas Leijdekkers