Introduction to Java Log components (Common-logging,log4j,slf4j,logback)

Source: Internet
Author: User
Tags log4j

Integration principles of slf4j and Jul, log4j1, log4j2 and Logback

Slf4j

Let's start with a simple use case to explain

2.1 Simple Use Cases

private static Logger Logger=loggerfactory.getlogger (Log4jslf4jtest.class);

public static void Main (string[] args) {

if (logger.isdebugenabled ()) {

Logger.debug ("slf4j-log4j debug Message");

}

if (logger.isinfoenabled ()) {

Logger.debug ("slf4j-log4j info message");

}

if (logger.istraceenabled ()) {

Logger.debug ("slf4j-log4j trace Message");

}

}

The above logger interface, Loggerfactory category are slf4j their own definition.

2.2 Principle of Use

Loggerfactory.getlogger (Log4jslf4jtest.class) has the following source codes:

public static Logger GetLogger (String name) {

Iloggerfactory iloggerfactory = Getiloggerfactory ();

return Iloggerfactory.getlogger (name);

}

The process of gaining log is roughly divided into 2 stages

The process of gaining iloggerfactory (literally the factory producing logger)

According to Iloggerfactory, the process of gaining logger

Here are some details:

1 The process of gaining iloggerfactory

It can also be divided into 3 processes:

1.1 Searching for Org/slf4j/impl/staticloggerbinder.class from the same path

Classloader.getsystemresources ("Org/slf4j/impl/staticloggerbinder.class")

If you find more than one, you can output Class path contains multiple slf4j bindings, which means that there are several logs that are actually bound to slf4j

Let's look at the output log (which simplifies some of the content) when there are many staticloggerbinder:

Slf4j:class path contains multiple slf4j bindings.

Slf4j:found binding in [Slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/staticloggerbinder.class]

Slf4j:found binding in [Logback-classic-1.1.3.jar!/org/slf4j/impl/staticloggerbinder.class]

Slf4j:found binding in [Slf4j-jdk14-1.7.12.jar!/org/slf4j/impl/staticloggerbinder.class]

Slf4j:see http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

Slf4j:actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

1.2 "Random Pick" a staticloggerbinder.class to create a single example

Staticloggerbinder.getsingleton ()

This "random pick" can be seen in official documents stating:

The SLF4J API is designed-bind with one and only one underlying logging the framework at a time. If more than one binding are present on the class path,

SLF4J would emit a warning, listing the location of those bindings

The warning emitted by SLF4J are just that, a warning. Even when multiple bindings is present,slf4j would pick one logging

Framework/implementation and bind with it. The slf4j picks a binding is determined by the JVM and for all practical purposes should be considered random

1.3 According to the above-created Staticloggerbinder, return a iloggerfactory example

Staticloggerbinder.getsingleton (). Getloggerfactory ()

So slf4j and other actual log frameworks in the Integrated Jar package will contain this kind of org/slf4j/impl/staticloggerbinder.class file, and provide a iloggerfactory of the reality

2 according to Iloggerfactory to obtain the logger of the process

This depends on the iloggerfactory type of the body, and the following integration is detailed to explain

3 SLF4J integration with jdk-logging

3.1 Required JAR Packages

Slf4j-api

Slf4j-jdk14

The response to Maven depends on:

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.12</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-jdk14</artifactId>

<version>1.7.12</version>

</dependency>

3.2 Use Cases

private static final Logger Logger=loggerfactory.getlogger (Julslf4jtest.class);

public static void Main (string[] args) {

if (logger.isdebugenabled ()) {

Logger.debug ("Jul debug Message");

}

if (logger.isinfoenabled ()) {

Logger.info ("Jul info message");

}

if (logger.iswarnenabled ()) {

Logger.warn ("Jul warn message");

}

}

The above-mentioned logger, loggerfactory are slf4j their own API in the content, there is no JDK logging of the shadow, and then the log is produced by the JDK from the logging to output, as follows:

April 28, 2015 7:33:20 pm com.demo.log4j.JulSlf4jTest Main

Info: Jul Info Message

April 28, 2015 7:33:20 pm com.demo.log4j.JulSlf4jTest Main

Warning: Jul warn message

3.3 Use case principle analysis

First look at the contents of the Slf4j-jdk14 jar package:

Jul integrates with slf4j

From the, you can see:

Is that there is a org/slf4j/impl/staticloggerbinder.class category

The iloggerfactory type that the Staticloggerbinder returns will be Jdk14loggerfactory

Jdk14loggeradapter is the logger interface of SLF4J definition.

The following carding the entire process:

1 The process of gaining iloggerfactory

With Org/slf4j/impl/staticloggerbinder.class in the same path, the Staticloggerbinder in Slf4j-jdk14 is chosen to create a single case and return to Iloggerfactory,

To see what type of iloggerfactory is in the Staticloggerbinder:

Private Staticloggerbinder () {

Loggerfactory = new Org.slf4j.impl.JDK14LoggerFactory ();

}

So we've returned the Jdk14loggerfactory case.

2 according to Iloggerfactory to obtain the logger of the process

Take a look at how Jdk14loggerfactory returns an example of a slf4j-definition logger interface, with the following source code:

Java.util.logging.Logger Jullogger = Java.util.logging.Logger.getLogger (name);

Logger newinstance = new Jdk14loggeradapter (Jullogger);

As you can see, it is the native way of using the JDK's logging to create a JDK's own Java.util.logging.Logger example, and take a look at the native notation of jdk-logging.

And then use Jdk14loggeradapter to package the above Java.util.logging.Logger into a logger example of slf4j definition.

So we use SLF4J to go through the process and eventually entrust the java.util.logging.Logger of the JDK to execute.

4 SLF4J integration with LOG4J1

4.1 Required JAR Packages

Slf4j-api

Slf4j-log4j12

Log4j

MAVEN depends on:

<!--slf4j--

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.12</version>

</dependency>


<!--slf4j-log4j--

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.12</version>

</dependency>


<!--log4j--

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

4.2 Use Cases

The first step: editing the Log4j.properties configuration file and putting it in the same path

Log4j.rootlogger = Debug, console

Log4j.appender.console = Org.apache.log4j.ConsoleAppender

Log4j.appender.console.layout = Org.apache.log4j.PatternLayout

Log4j.appender.console.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss}%m%n

The details of the profile are not the focus of this blog, no longer explain, search by yourself

Step Two: Use the code as follows

private static Logger Logger=loggerfactory.getlogger (Log4jslf4jtest.class);

public static void Main (string[] args) {

if (logger.isdebugenabled ()) {

Logger.debug ("slf4j-log4j debug Message");

}

if (logger.isinfoenabled ()) {

Logger.info ("slf4j-log4j info message");

}

if (logger.istraceenabled ()) {

Logger.trace ("slf4j-log4j trace Message");

}

}

Supplemental stating:

1 configuration files can also be placed in an arbitrary manner, such as log4j1 native mode to load the configuration file log4j1 native

2 Note the difference of the two ways:

Slf4j:logger Logger=loggerfactory.getlogger (Log4jslf4jtest.class);

Log4j:logger Logger=logger.getlogger (Log4jtest.class);

The logger of SLF4J is the interface of SLF4J definition, and the logger of log4j is a kind. Loggerfactory is slf4j himself.

4.3 Use case principle analysis

First look at the contents of the SLF4J-LOG4J12 package:

log4j Integration with SLF4J

Is that there is a org/slf4j/impl/staticloggerbinder.class category

The iloggerfactory type that the Staticloggerbinder returns will be Log4jloggerfactory

Log4jloggeradapter is the logger interface of SLF4J definition.

To see the process:

1 access to Iloggerfactory

From the slf4j of the above, we know that Iloggerfactory is created by Staticloggerbinder, so it can be divided into 2 simple processes:

1.1 First process: SLF4J looking for binding Staticloggerbinder

Use ClassLoader to download the "Org/slf4j/impl/staticloggerbinder.class" kind of URL, and then found the SLF4J-LOG4J12 package Staticloggerbinder

1.2 The second process: Create Staticloggerbinder and create Iloggerfactory

The source codes are as follows:

Staticloggerbinder.getsingleton (). Getloggerfactory ()

Taking the staticloggerbinder of SLF4J-LOG4J12 as an example, the iloggerfactory created is Log4jloggerfactory

2 according to Iloggerfactory to obtain the logger of the process

Take a look at how Log4jloggerfactory returns an example of a slf4j-definition logger interface, with the following source code:

Org.apache.log4j.Logger Log4jlogger;

if (Name.equalsignorecase (logger.root_logger_name))

Log4jlogger = Logmanager.getrootlogger ();

Else

Log4jlogger = Logmanager.getlogger (name);


Logger newinstance = new Log4jloggeradapter (Log4jlogger);

2.1 We can see the native way through the log4j1, that is, using the log4j1 Logmanager to obtain, the introduction of the log4j1 load configuration file, and then initialize,

Finally return to a Org.apache.log4j.Logger Log4jlogger, see the log4j1 of the native writing method

2.2 The above Org.apache.log4j.Logger Log4jlogger is sealed into log4jloggeradapter, and Log4jloggeradapter is the interface of SLF4J,

So we use the SLF4J Logger interface example (which is log4jloggeradapter) that is delegated to the internal Org.apache.log4j.Logger example

5 SLF4J integration with LOG4J2

5.1 Required JAR Packages

Slf4j-api

Log4j-api

Log4j-core

Log4j-slf4j-impl (for log4j2 and SLF4J integration)

The response to Maven depends on:

<!--slf4j--

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.12</version>

</dependency>

<!--log4j2--

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-api</artifactId>

<version>2.2</version>

</dependency>

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-core</artifactId>

<version>2.2</version>

</dependency>

<!--Log4j-slf4j-impl--

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-slf4j-impl</artifactId>

<version>2.2</version>

</dependency>

5.2 Use Cases

The first step: editing the Log4j2 configuration file Log4j2.xml, simple as follows:,

<?xml version= "1.0" encoding= "UTF-8"?>

<configuration status= "WARN" >

<Appenders>

<console name= "Console" target= "System_out" >

<patternlayout pattern= "%d{hh:mm:ss. SSS} [%t]%-5level%logger{36}-%msg%n "/>

</Console>

</Appenders>

<Loggers>

<root level= "Debug" >

<appenderref ref= "Console"/>

</Root>

</Loggers>

</Configuration>

Step two: How to use

private static Logger Logger=loggerfactory.getlogger (Log4j2slf4jtest.class);

public static void Main (string[] args) {

if (logger.istraceenabled ()) {

Logger.trace ("slf4j-log4j2 trace Message");

}

if (logger.isdebugenabled ()) {

Logger.debug ("slf4j-log4j2 debug Message");

}

if (logger.isinfoenabled ()) {

Logger.info ("SLF4J-LOG4J2 info message");

}

}

5.3 Use Case principle analysis

First look at the contents of the Log4j-slf4j-impl package:

log4j Integration with SLF4J

Is that there is a org/slf4j/impl/staticloggerbinder.class category

The iloggerfactory type that the Staticloggerbinder returns will be Log4jloggerfactory ( The log4jloggerfactory here is not the same as the log4jloggerfactory of the above log4j1 integration)

Log4jlogger is the logger interface of SLF4J definition.

To see the process:

1 access to Iloggerfactory

1.1 First process: SLF4J looking for binding Staticloggerbinder

Use ClassLoader to download the "Org/slf4j/impl/staticloggerbinder.class" kind of URL, and then found the Log4j-slf4j-impl package Staticloggerbinder

1.2 The second process: Create Staticloggerbinder and create Iloggerfactory

The iloggerfactory returned by Staticloggerbinder in the Log4j-slf4j-impl package is log4jloggerfactory

2 according to Iloggerfactory to obtain the logger of the process

Take a look at how Log4jloggerfactory returns an example of a slf4j-definition logger interface, with the following source code:

@Override

Protected Logger Newlogger (final String name, Final Loggercontext context) {

Final String key = Logger.ROOT_LOGGER_NAME.equals (NAME)? LogManager.ROOT_LOGGER_NAME:name;

return new Log4jlogger (Context.getlogger (key), name);

}


@Override

Protected Loggercontext GetContext () {

Final class<?> anchor = Reflectionutil.getcallerclass (FQCN, package);

return anchor = = null? Logmanager.getcontext (): GetContext (Reflectionutil.getcallerclass (anchor));

}

2.1 We can see the native way through log4j2, that is, using Log4j2 's loggercontext to obtain

Returns an example of a org.apache.logging.log4j.core.Logger-log4j2 definition of a logger interface, and sees Log4j2 native notation

2.2 The above Org.apache.logging.log4j.core.Logger is sealed into Log4jlogger, and Log4jlogger is the logger interface of SLF4J,

So the example of the SLF4J logger interface that we use (which is Log4jlogger) will be delegated to the logger example of log4j2 definition within the inner part.

The process of gaining loggercontext is also the native way of Log4j2:

Logmanager.getcontext ()

The operation will download the LOG4J2 configuration file, initiating the LOG4J2 initialization

6 SLF4J integration with Logback

6.1 Required JAR Packages

Slf4j-api

Logback-core

Logback-classic (already contains integration pack for SLF4J)

The response to Maven depends on:

<!--Slf4j-api--

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.12</version>

</dependency>

<!--Logback--

<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>

6.2 Use Cases

The first step: editing the Logback configuration file Logback.xml, simple as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<configuration>

<appender name= "STDOUT" class= "Ch.qos.logback.core.ConsoleAppender" >

<encoder>

<pattern>%d{hh:mm:ss. SSS} [%thread]%-5level%logger{36}-%msg%n</pattern>

</encoder>

</appender>

<root level= "DEBUG" >

<appender-ref ref= "STDOUT"/>

</root>

</configuration>

Step two: How to use

private static final Logger Logger=loggerfactory.getlogger (Logbacktest.class);


public static void Main (string[] args) {

if (logger.isdebugenabled ()) {

Logger.debug ("slf4j-logback debug Message");

}

if (logger.isinfoenabled ()) {

Logger.info ("slf4j-logback info message");

}

if (logger.istraceenabled ()) {

Logger.trace ("Slf4j-logback trace Message");

}

}

6.3 Use case Principle analysis

First look at the contents of the Logback-classic package with the SLF4J integration:

log4j Integration with SLF4J

log4j Integration with SLF4J

Is that there is a org/slf4j/impl/staticloggerbinder.class category

The iloggerfactory type that the Staticloggerbinder returns will be Loggercontext (the logback of the image)

The Ch.qos.logback.classic.Logger of Logback's own definition is the logger interface of SLF4J definition.

1 access to Iloggerfactory

1.1 First process: SLF4J looking for binding Staticloggerbinder

Use ClassLoader to download the "Org/slf4j/impl/staticloggerbinder.class" kind of URL, and then found the Logback-classic package Staticloggerbinder

1.2 The second process: Create Staticloggerbinder and create Iloggerfactory

The iloggerfactory returned by Staticloggerbinder in the Logback-classic package is Loggercontext (the Logback's image)

After creating a single example, the Logback is initialized at the same time, and Logback is going to look for a series of configuration files to try and parse.

2 according to Iloggerfactory to obtain the logger of the process

Take a look at how Loggercontext (Logback's image) returns an example of the logger interface of a SLF4J definition:

The Ch.qos.logback.classic.Logger (Logback's native Logger pair) returned by the Loggercontext (Logback's image) is slf4j Logger reality.


Introduction to Java Log components (Common-logging,log4j,slf4j,logback)

Related Article

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.