This article is the third in a series on SpringBoot’s logging support. It will explain the logging in Java platform, and thoroughly uncover the logging framework in the process of use
Logging framework ramble
Many logging frameworks exist under the Java platform, such as JUL (java.util.logging), JCL (Apache Commons Logging), Log4j, Log4j2, Logback, SLF4j, jboss-Logging, and so on.
Java platform under the development of logging framework, through the specific log implementation, and later developed to log facade + log implementation.
The Java log
When developing, we should use the API provided by the logging facade rather than directly using the concrete logging implementation.
For example, if we use slf4J +log4j, we can configure the log4j configuration (such as log4j.xml) and use the logging API provided by SLf4J. That is, each logging implementation framework has its own logging configuration file, which is still provided even after the logging facade is used. (That is, the logging facade only implements API adaptation, and there is no unified specification for logging configuration)
Currently the most popular log facade is SLF4J, we take this as an example to specific analysis, mainly for two points of analysis:
First: How does SLF4J fit the various logging implementations?
Second: How can logs be used uniformly across projects?
slf4j+logback
slf4j+log4j
Slf4j +logback: slf4j+log4j: slf4j+log4j
First, let’s look at the first issue, how SL4J fits in with other logging implementation frameworks.
Simply look at the left side of the diagram and provide the corresponding JARS (some of which require additional adaptation JARS in addition to the specific logging implementation).
Second, we need to be aware of the fact that other dependencies inevitably need to be introduced in project development. For example, Spring uses Commons-logging by default and Hibernate uses jboss-logging by default. How do we unify the way we log?
For example, if we rely on Spring, even though Spring uses JCL by default, it is easy to use SLF4J + LogBack in our project. We first remove the JCL dependency. Then we can introduce the jCL-over-slf4j.jar dependency. Jcl-over-slf4j. jar and JCL are identical in package/class/method names, but jCL-over-slf4J internally calls the SLf4J API to avoid Spring errors. You can change the way Spring logging is done!)
How does SpringBoot handle logs uniformly
SpringBoot can automatically adapt all logging, and the underlying use of SLF4J +logback logging, when introducing other frameworks, just need to exclude the logging framework that this framework depends on.
SpringBoot is smart enough to have introduced some logging adaptation dependencies, so let’s take a look.
Springboot log dependency
I think you can see springBoot’s best intentions by looking at the log dependencies above!
Spring’s default logging dependency is JCL. Spring’s default logging dependency is JCL. Spring’s default logging dependency is JCL.
Springboot rely on spring
Spring has eliminated JCL dependencies
SpringBoot log usage
On the use of SpringBoot logs, the following points are mainly pointed out:
First: some common global configurations, such as format/path/level
Second: use @slf4j for logging
Third: log profile function
Fourth: Switch logging frameworks
Log configuration in application.properties
Specify the log configuration file path logging.config= CLASSPath :logback.xml
Logging. path specifies the log path
Specify the logging level, either directly in application.properties:
logging.level.x.y.z=info
Or configure it in logback.xml.
About the specific configuration of logs, here is not detailed, there are a lot of information you can refer to.
The specific use of log, I believe you are very clear, basically is to get the log object:
private final Logger logger = LoggerFactory.getLogger(XXX.class);
But for simplicity lombok provides annotations for logging. (Lombok plug-ins for IDEA and lombok dependencies are required)
Lombok supports
Provide annotations, using log directly
For example, instead of calling logback. XML, you can call logback-spring. XML, and springBoot will handle the logging configuration. You can use the Profile function.
logback-spring.xml
Finally, what if you don’t want to use slf4J +logback, which springBoot supports by default, but slf4J + XXX?
It is very simple, according to the above theory, first, exclude dependence (logback); Second, add dependencies (log4j); Third, add a specific logging configuration (log4j.xml).
conclusion
Well, here, we will take SpringBoot as the breakthrough point, the log of the entire Java platform for an analysis, I believe that later in the development process encountered log related problems, to know ~