Seamless migration from log4j to log logback

logback contrast log4j a bit in this not go into details.

Since the code in the original project, a large number of logs generated by the new code log4j achieved by logback want to generate a log of the way, and hope the old code will be generated in the log without modification directly to the specified configuration logback in.

log4j migrate to logback  article introduces a migration patterns, but the need to modify the code, this migration risk is too high.

"SLF4J official documents" in the conventional bridge API  described in the article using log4j-over-slf4j.jar Alternatively log4j.jar, to generate the original log logback configuration by way of the bridge.

log4j-over-slf4j.jar Download: Log4j Implemented The Over SLF4J  ,  Maven central repository Fast Facts

Old logging code generated using log4j

Copy the code
package com.spike.test;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class Log4jExample {
       static Logger log = Logger.getLogger(Log4jExample.class.getName());
       
       public static void main(String[] args)throws IOException,SQLException{
          log.debug("Debug");
          log.info("Info");
          log.error("error");
          System.out.println(Log4jExample.class.getName()+" test");
       }
}
Copy the code

The log4j-over-slf4j.jar introduced into items, excluding the log4j.jar, configured logback environment needed to generate a log file in a specified configuration logback.xml

2016-11-03 14:14:31,799 [DEBUG] [main] com.spike.test.Log4jExample [LogbackExample.java : 18] Debug
2016-11-03 14:14:31,803 [INFO] [main] com.spike.test.Log4jExample [LogbackExample.java : 19] Info
2016-11-03 14:14:31,803 [ERROR] [main] com.spike.test.Log4jExample [LogbackExample.java : 20] error
com.spike.test.Log4jExample test

So far, migration to logback success without changing the code conditions.

But we found a small problem, for example, is inherited HTMLLayout, Layout class can not work.

 

 

reference:

log4j-over-slf4j coexist with slf4j-log4j12 stack overflow exception analysis

Log4j troubleshooting -log4j, logback in the end use to which?

Guess you like

Origin www.cnblogs.com/exmyth/p/11082224.html