Skip to main content

Migrate to SLF4J from Log4j

In this guide, we'll use OpenRewrite to perform an automated migration from Apache Log4j (handling both log4j 1.x or log4j 2.x) to the Simple Logging Facade for Java (SLF4J).

Example Configuration

See various ways you can configure your project to run this recipe on the recipe reference page. This is also where you can find the full list of changes it will make.

Once you've configured your project, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.

Before and After

Similar to the SLF4J Migrator, the goal of the Log4jToSlf4j recipe is to significantly reduce the amount of work involved in migrating Log4j to SLF4J. Because OpenRewrite works by leveraging type-attributed Lossless Semantic Tree (LST) representing your source code, the Log4jToSlf4j recipe can overcome several limitations discussed in the SLF4J Migrator guide.

.toString()

Because Log4j logging statements are automatically migrated to use the SLF4J Parameterized Logging equivalent (see "Use SLF4J Parameterized Logging"), messages of type String are supported. Therefore, there is no need to add a .toString() method invocation on the object.

import org.apache.log4j.Logger;

class Example {
Logger logger = Logger.getLogger(Example.class);

void method(Example example) {
logger.info(example);
logger.info(new Object());
logger.info(new StringBuilder("append0"));
}
}

FATAL to ERROR

The Log4j log level FATAL is not supported in SLF4J. Therefore, FATAL level log statements are migrated to ERROR level.

import org.apache.log4j.Logger;

class Example {
Logger logger = Logger.getLogger(Example.class);

void method() {
logger.fatal("uh oh");
}
}

Migrate using type information

A method declaring multiple loggers on the same line will have a complete conversion.

import org.apache.log4j.Logger;

class Example {
public void someMethod(Logger l1, Logger l2) {
}
}

Handle Exception Migrations

Additionally, Log4j ERROR log level exceptions are migrated to their SLF4J equivalent:

import org.apache.log4j.Logger;

class Example {
Logger logger = Logger.getLogger(Example.class);

void method(String numberString) {
try {
Integer i = Integer.valueOf(numberString);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}

For the full list of changes, see the recipe's reference documentation.

Known Limitations

The following is a list of known limitations/issues:

  • Currently, log4j properties and configuration files are not migrated.
  • Log4j configuration customizations which do not have an SLF4J equivalent cannot be migrated.

We are always looking for feedback on which tasks should be prioritized. If you have a specific use case that is not yet covered by this project, please reach out to our team or submit a PR to help out the community.