Relationships between the individual log frames

Source: Internet
Author: User

1. General overview


As you can see, commons-logging and slf4j are both log interfaces for users to use without providing implementations.

Log4j,logback and so is the real realization of the log

So, basically is the interface + concrete implementation of the way to use


2. Integration among the various frameworks

(1) SLF4J and LOG4J1 integration

1) Maven

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactid>slf4j-log4j12</ artifactid>
	<version>1.7.12</version>
</dependency>
2) Jar Pack

Description: The SLF4J-API is slf4j itself api;log4j is the realization of log4j itself; Slf4j-log4j12 is the slf4j and log4j integration package

(2) SLF4J and Logback integration

1) Maven

        <dependency>
		<groupId>org.slf4j</groupId>
		<artifactid>slf4j-api</artifactid >
		<version>1.7.7</version>
	</dependency>

	<dependency>
		<groupid >ch.qos.logback</groupId>
		<artifactId>logback-classic</artifactId>
		<version >1.1.3</version>
	</dependency>

	<dependency>
		<groupid>ch.qos.logback </groupId>
		<artifactId>logback-core</artifactId>
		<version>1.1.3</version >
	</dependency>
2) Jar

3. Switching between log systems

(1) log4j switch to Logback if we use the LOG4J API in the code to log output, we now want to make the actual log output through Logback without changing the code.

Jar package already in use: log4j

Use case:

Import Org.apache.log4j.Logger;
Import Org.junit.Test;

public class Log4jtest {
	private static final Logger Logger = Logger.getlogger (log4jtest.class);

	@Test public
	void Test () 
	{
		Logger.info (' This is log4j ');
	}

Replace step:

1 Remove the log4j Jar pack

2) Add the following Jar pack

1>LOG4J-OVER-SLF4J (implement log4j switch to SLF4J)

2>slf4j-api

3>logback-core (Logback Implementation)

4>logback-classic

or write Maven dependencies directly.

<properties>
  	<org.slf4j-version>1.7.7</org.slf4j-version>
</properties>
....
<dependency>  
    <groupId>org.slf4j</groupId>  
    <artifactid>slf4j-api</artifactid >  
    <version>${org.slf4j-version}</version>  
</dependency>  

<dependency>  
    <groupId>org.slf4j</groupId>  
    <artifactId>log4j-over-slf4j</artifactId>  
    <version>${org.slf4j-version}</version>  
</dependency>  

<dependency>
    < groupid>ch.qos.logback</groupid>
    <artifactId>logback-core</artifactId>
    < version>1.1.3</version>
</dependency>
<dependency>
    <groupId> ch.qos.logback</groupid>
    <artifactId>logback-classic</artifactId>
    <version> 1.1.3</version>
</dependency>

3) Add Logback.xml configuration file

This enables you to switch log4j to logback without changing any of the code


(2) common-logging switch to Logback if we use the Common-logging API in the code to log output, we now want to make the actual log output through Logback without changing the code.

Jar package already in use: common-logging-1.1.3

Use case:

Import Org.apache.commons.logging.Log;
Import org.apache.commons.logging.LogFactory;
 
public class Jcltest {
    private static log = Logfactory.getlog (Jcltest.class);
    
    @Test public
    void Test ()
    {
    	log.debug ("Debug Info.");
        Log.info ("info info");
        Log.warn ("Warn info");
        Log.error ("error info");
        Log.fatal ("Fatal info");
    }

Replace step:

1 Remove the Common-logging jar bag (actually not going to do it)

2) Add the following Jar pack

1>JCL-OVER-SLF4J (implement lcommon-logging switch to SLF4J)

2>slf4j-api

3>logback-core (Logback Implementation)

4>logback-classic

or write Maven dependencies directly.

<dependency>  
    <groupId>org.slf4j</groupId>  
    <artifactid>slf4j-api</artifactid >  
    <version>${org.slf4j-version}</version>  
</dependency>  

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    < version>1.6.1</version>
</dependency>
<dependency>
    <groupId> ch.qos.logback</groupid>
    <artifactId>logback-core</artifactId>
    <version> 1.1.3</version>
</dependency>
<dependency>
    <groupid>ch.qos.logback</ groupid>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version >
</dependency>

3) Add Logback.xml configuration file

This enables you to switch common-logging to logback without changing any of the code

4. Harmonization between different log systems

Because many other frameworks may be introduced into the project, these frameworks are likely to use a different log framework, such as Log4j,logback,common-logging, Java.util.logging and so on, and even if the same log jar version may be different, resulting in a very confusing project log system.

This time you want to unify the use of a log system to implement, this time you can use the following configuration:

                <!--log--> <dependency> <groupId>org.slf4j</groupId> <artifactid>s Lf4j-api</artifactid> <version>${org.slf4j-version}</version> </dependency> < dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <v ersion>${org.slf4j-version}</version> </dependency> <dependency> <groupid>org.slf4j </groupId> <artifactId>log4j-over-slf4j</artifactId> <version>${org.slf4j-version}</ version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactid>ju L-to-slf4j</artifactid> <version>${org.slf4j-version}</version> </dependency> < Dependency> <groupId>org.jboss.logging</groupId> <artifactid>jboss-logging</artifactid > <version>3.1.4.GA</version> </depenDency>
 
Several adapters are configured in it: LOG4J-OVER-SLF4J, jcl-over-slf4j, jul-to-slf4j.

Then the jboss-logging section also has a unified version control (because dependency delivery uses the closest dependencies to the project)

The rest is to configure the Logback

Note: June needs to execute a line of initialization code extra

Slf4jbridgehandler.install ()//June to SLF4J



5. Conflict between log Systems

Let's summarize the related jar packs:

(1) log4j-over-slf4j, jcl-over-slf4j, jul-to-slf4j, and other similar from-over-slf4j-named Jars, are switched from a log system to a slf4j

(2) Slf4j-jcl,slf4j-log4j12 and other similar slf4j-to named Jar, are switched from SLF4J to a log system

So obviously there are the following 2 cases:

1) conflict between JCL-OVER-SLF4J and SLF4J-JCL

jcl-over-slf4j:commons-logging Switch to SLF4J

SLF4J-JCL:SLF4J Switch to Commons-logging

If the two coexist, they will inevitably lead to mutual entrustment, resulting in memory overflow

2) conflict between LOG4J-OVER-SLF4J and SLF4J-LOG4J12

LOG4J-OVER-SLF4J:LOG4J1 Switch to SLF4J

SLF4J-LOG4J12:SLF4J Switch to Log4j1

If the two coexist, it will inevitably lead to mutual entrustment, resulting in memory overflow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.