log4j switch logback

1 Replace the jar package    

    Required jar package:

        logback-access-0.9.24.jar

        logback-classic-0.9.24.jar

        logback-core-0.9.24.jar

        slf4j-api-1.6.1.jar

        jcl-over-slf4j-1.6.1.jar (this jar is used to be compatible with org.apache.commons.logging.* in dependency packages such as Spring)

     Jar packages that need to be removed:

         log4j-1.2.17.jar 

         slf4j-log4j12-1.7.2.jar (if any, also delete)

 2 Replace the log configuration file

      The log4j framework requires log4j.properties, and the logback configuration file is in logback.xml format. If your configuration file is log4j.properties, then congratulations, it's very simple, just convert directly to the following address:

     log4j.properties to logback.xml Translator   (http://logback.qos.ch/translator/) is enough . If not, then you have to write the logback.xml configuration yourself. Then put logback.xml in the corresponding read path and delete the log4j configuration file.

 

3 Code conversion

   

    If you use org.apache.commons.logging.* congratulations, you can complete the migration without changing the code at all (refer to jcl-over-slf4j-1.6.1.jar).

    If you are using org.apache.log4j.Logger, then modify the code:

    1)     import org.apache.log4j.Logger;  importorg.slf4j.Logger;    import org.slf4j.LoggerFactory;

    2)     private static Logger LOG =Logger.getLogger(ServerRunner.class);   private static Logger LOG =LoggerFactory.getLogger(ServerRunner.class);

    3) If you have packaged log4j, you can only replace one place; if not, I am afraid that every class file that outputs logs will have to be changed. The workload is very large, and a text replacement tool is recommended: SearchAndReplace. At the same time, it is recommended to encapsulate a layer of logback to facilitate maintenance.

 

 

4 test classes

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class LogbackDemo {

    private static Logger log = LoggerFactory.getLogger(LogbackDemo.class);

 

    public static void main(String[] args) {

        log.trace("======trace");

        log.debug("======debug");

        log.info("======info");

        log.warn("======warn");

        log.error("======error");

 

    }

 

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939478&siteId=291194637