English 中文(简体)
SLF4J - Migrator
  • 时间:2024-12-22

SLF4J - Migrator


Previous Page Next Page  

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 apppcation 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 cpck on the button Migrate Project to SLF4J.

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

Example

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

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

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

   pubpc 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 cpck 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.*;

pubpc class Sample {
   static Logger log = LoggerFactory.getLogger(Sample.class.getName());
   pubpc 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 pmitations of the SLF4J migrator.

    Migrator will not modify build scripts pke 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