SLF4J - Migrator



If you have a project in Jakarta Commons Logging (JCL) or, log4j or, java.util.logging (JUL) and you want to convert these projects to SLF4J, you can do so using the migrator tool provided in the SLF4J distribution.

Migrator

Running SLF4J Migrator

SLF4J is a simple single jar file (slf4j-migrator.jar) and you can run it using the java –jar command.

To run it, in command prompt, browse through the directory where you have this jar file and execute the following command.

java -jar slf4j-migrator-1.8.0-beta2.jar
Starting SLF4J Migrator

This starts the migrator and you can see a standalone java application as −

Migrator Project

As specified in the window, you need to check the type of migration you want to do and select the project directory and click on the button Migrate Project to SLF4J.

This tool goes to the source files you provide and performs simple modifications like changing the import lines and logger declarations from the current logging framework to SLF4j.

Example

For example, let us suppose we have a sample log4j(2) project in eclipse with a single file as follows −

import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class Sample {
   /* Get actual class name to be printed on */
   static Logger log = Logger.getLogger(Sample.class.getName());

   public static void main(String[] args)throws IOException,SQLException {
      log.debug("Hello this is a debug message");
      log.info("Hello this is an info message");
   }
}

To migrate the sample log4j(2) project to slf4j, we need to check the radio button from log4j to slf4j and select the directory of the project and click Exit to migrate.

Directory Of The Project

The migrator changed the above code as follows. Here if you observe the import and logger statements have been modified.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class Sample {
   static Logger log = LoggerFactory.getLogger(Sample.class.getName());
   public static void main(String[] args)throws IOException,SQLException {
      log.debug("Hello this is a debug message");
      log.info("Hello this is an info message");
   }
}

Since you already have log4j.jar in your project, you need to add slf4j-api.jar and slf4jlog12.jar files to the project to execute it.

Limitations of SLF4JMigrator

Following are the limitations of the SLF4J migrator.

  • Migrator will not modify build scripts like ant, maven and, ivy you need to do it yourself.

  • Migrator does not support messages other than the String type.

  • Migrator does not support the FATAL level.

  • While working with log4j, migrator will not migrate calls to PropertyConfigurator or DomConfigurator.

Advertisements