springboot-Log Frame

Source: Internet
Author: User

1. Introduction to the Log framework

Logging is an essential part of an application. Online problem tracking, based on log business logic statistical analysis and so can not be separated from the log. There are many well-written and mature log frameworks in the Java domain, often used with JCL (Jakarta Commons logging), slf4j (simple logging facade Java), jboss-logging, log4j, JUL ( java.util.logging), Log4j2, Logback and so on.

From the implementation, the Java framework is divided into two kinds, one is the log façade (the abstraction layer of the log) and the log implementation, so when we select the log frame, it is generally selected a log façade, and then choose a log implementation. Among them, the log façade includes JCL (Jakarta Commons logging), slf4j (simple logging facade Java), jboss-logging, etc., while the log implementation includes log4j, JUL ( java.util.logging), Log4j2, Logback.

  The spring framework uses the JCL by default, the implementation layer can choose log4j or log4j2, and spring Boot chooses slf4j and Logback

2, the relationship between the log framework

  Because the log framework does not form a unified interface, we can choose the log façade and log implementation when the wrong choice. and the log abstraction interface is basically divided into two camps, one is JCL (Jakarta Commons logging), the other is slf4j.

Commons logging and slf4j are log facades (façade mode is a software design pattern commonly used in software engineering, also known as positive mode and appearance mode. It provides a unified high-level interface for a set of interfaces in a subsystem, making the subsystem easier to use. Log4j and Logback are the specific log implementation scenarios. Can be simply understood as interface and interface implementation, call this only need to pay attention to the interface without focusing on the specific implementation, to do decoupling.

One of the more commonly used combinations is the combination of slf4j and Logback (springboot), Commons logging combined with log4j (Spring).

Logback must be used in conjunction with SLF4J. Since Logback and slf4j are the same authors, their compatibility is self-evident.

3, slf4j use

  At the time of development, the logging method call should not be called directly to the implementation class of the log, but instead call the method inside the log abstraction layer to import the implementation jar package of SLF4J and Logback to the system.

Import Org.slf4j.Logger; Import org.slf4j.LoggerFactory;  Public class HelloWorld {  publicstaticvoid  main (string[] args) {    = Loggerfactory.getlogger (HelloWorld.  Class);    Logger.info ("Hello World");}  }
View Code

What do we do if we want to use other frameworks for Springboot? Such as

  

For example, we first use the LOG4J framework log in Springboot, not only to import the Log4j.jar package, but also to import the Slf4j-log412.jar package, which is an adapter, the Slf4 abstraction interface and log4j to adapt. Each log implementation framework has its own configuration file, after using SLF4J, the configuration file is made into a log implementation framework configuration file.

4. Log level

Log level from low to High: Trace<debug<info<warn<error, the log output is greater than or equal to the current set of log level, such as I set the log level of info, then output info, Warn, and error level log information, trace and debug will be ignored. We can also set the log to write to the file, such as configuration Logging.file=d:/springboot.log, will generate a Springboot.log file on the D disk. Logging.path is configured to create a log file in which folder.

    

springboot-Log Frame

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.