Slf4j 简明教程

SLF4J - Migrator

如果您在 Jakarta Commons Logging (JCL) 或 log4j 或 java.util.logging (JUL) 中有一个项目,并且您希望将这些项目转换为 SLF4J,则可以使用 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 是一个简单的单 jar 文件 (slf4j-migrator.jar),并且您可以使用 java –jar 命令运行它。

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

若要运行它,在命令提示符中,浏览到存放此 jar 文件的目录并执行以下命令。

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

这将启动迁移程序,可看到独立的 Java 应用程序,如下所示:

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

migrator project

如窗口中指定,您需要检查要执行的迁移类型,并选择项目目录,然后单击按钮“将项目迁移到 SLF4J”。

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.

该工具会转到您提供的源文件,并执行简单的修改,例如将导入行和记录器声明从当前日志记录框架更改为 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

例如,让我们假设我们有一个带一个文件的 log4j(2) 示例项目,如下所示:

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");
   }
}

若要将示例 log4j(2) 项目迁移到 SLF4j,我们需要选中单选按钮 from log4j to slf4j ,并选择项目目录,然后点击 Exit 进行迁移。

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

迁移程序将按如下方式更改上述代码。此处,如果您观察,import 和 logger 语句已经过修改。

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");
   }
}

因为您的项目中已存在 log4j.jar ,所以您需要将 slf4j-api.jarslf4jlog12.jar 文件添加到项目中来执行它。

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

以下是 SLF4J 迁移程序的局限性。

Following are the limitations of the SLF4J migrator.

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

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

  3. Migrator does not support the FATAL level.

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