Slf4j, slf4jlog4j

Source: Internet
Author: User

Slf4j, slf4jlog4j

Java Simple log appearance (Simple Logging Fa C ade for Java, slf4j) is another log framework (such as java. util. A simple appearance or abstraction of logging, logback, and log4j. Different log frameworks are used with the same API, and your favorite log frameworks are inserted during deployment.

 

Slf4j dependencies are slf4j-api.jar, and slf4j does not perform any operations by default if no framework is bound on the class path.

 

Hello World

The following example illustrates how to output "Hello world" in slf4j in the simplest way ".

Import org. slf4j. Logger;

Import org. slf4j. LoggerFactory;

 

Public class HelloWorld {

Public static void main (String [] args ){

Logger logger = LoggerFactory. getLogger (HelloWorld. class );

Logger.info ("Hello World ");

}

}

If you only add a slf4j-api.jar to a path, the following information appears after running:

SLF4J: Failed to load class "org. slf4j. impl. StaticLoggerBinder ".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Because slf4j is not bound to any log framework, if you add the slf4j-simple.jar to the class path:

  • Slf4j-api-1.7.12.jar
  • Slf4j-simple-1.7.12.jar

After compilation, the following output is displayed on the console:

[Main] INFO cn. ac. dicp. slf4j. HelloWorld-Hello World

 

Typical use Mode

The following simple code demonstrates the typical use mode of slf4j.

Import org. slf4j. Logger;

Import org. slf4j. LoggerFactory;

 

Public class Wombat {

Final Logger logger = LoggerFactory. getLogger (Wombat. class );

Integer t;

Integer oldT;

Public void setTemperature (Integer tem ){

OldT = t;

T = tem;

Logger. debug ("temperature set to {}. Old temperature was {}.", toString (), oldT );

If (tem. intValue ()> 50 ){

Logger.info ("Temperature has risen abve 50 degrees .");

}

}

}

Note the above '{}'.

 

Bind log framework

Slf4j supports multiple log frameworks:

Slf4j-log4j12-1.7.12.jar

Bind log4j 1.2, a widely used log framework. Add log4j. jar.

Slf4j-jdk14-1.7.12.jar

Bind java. util. logging, also known as JDK 1.4 logging.

Slf4j-nop-1.7.12.jar

Bind NOP and silently discard all logs.

Slf4j-simple-1.7.12.jar

Bind simple implementation and output all logs to System. err. Only information output with a level higher than or equal to INFO.

Slf4j-jcl-1.7.12.jar

Bind Jakarta Commons Logging.

Logback-classic-1.0.13.jar

Logback-core-1.0.13.jar required.

In addition to slf4j, native implementation also has its own bind mining project. For example, logback implements slf4j locally. The ch. qos. logback. classic. Logger class of logback directly implements the org. slf4j. Logger interface of slf4j. Therefore, the memory and computing workload used in combination with slf4j and logback are almost zero.

 

Change the log framework to directly change the package of the class path, such as replacing the slf4j-jdk14-1.7.12.jar with a slf4j-log4j12-1.7.12.jar. Slf4j can only bind one framework at a time. Do not place two or more connection jar files in the path.

 

Slf4j interfaces and adapters are extremely simple.

 

Libraries

The authors of those widely used components and libraries use the slf4j interface to avoid the log framework that end users must use. Users can select their favorite log framework during deployment, change the corresponding connection jar. This method is simple and effective.

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.