The log is introduced

concept

In computing, logs are used to record events that occur in a running operating system or other software, or to record messages sent between users of Internet chat software.

Log Request level

When we say log level, we usually mean log request level. For example,.info().error() refers to the log request level, which indicates the “importance” of the information in ().

There is no uniform log request level, and different logging facade and logging implementation frameworks define different log levels.

For example, Log4j2 has eight log request levels: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, and ALL (reference).

There are six log request levels of Log4j and Commons-logging: FATAL, ERROR, WARN, INFO, DEBUG, and TRACE

SLF4J and Logback log request levels: ERROR, WARN, INFO, DEBUG, TRACE(reference)

It is recommended to use the ERROR, WARN, INFO, and DEBUG levels as much as possible, as they are common to all logging facade \ implementation frameworks

Common logging frameworks in Java

Evolution of the logging framework

  • In early 1996, the EU SEMPER project team decided to write its own Tracing API. Over time, the API has become a very popular Java logging package, Log4j. Log4j later became part of an Apache Foundation project.
  • Log4j has become a near-standard for logging in the Java community since its release. The Apache Foundation is also said to have suggested that Sun introduce Log4j into the Java standard library, but Sun declined.
  • In 2002, With the release of Java1.4, Sun introduced its own Logging library, JUL(Java Util Logging), which basically mimics Log4j’s implementation. Log4j was already a mature technology before JUL came out, giving it an advantage in terms of choice.
  • In 2005, Apache introduced JCL(Jakarta Commons Logging). JCL simply defines a Logging interface (with a Simple implementation of Simple Log inside) to dynamically load Logging components at runtime. That is, in your application code, Simply invoke the Commons Logging interface, and the underlying implementation can be either Log4J or Java Util Logging.
  • In 2006, Ceki Gulcu, who originally wrote Log4j, developed SLF4J(The Simple Logging Facade for Java) as a more reliable alternative to JCL(Jakarta Commons Logging). After that, he developed Logback framework (Slf4j implementation), and went back to Sweden to set up QOS company. QOS official website describes Logback as follows: The Generic, Reliable Fast&Flexible Logging Framework is a Generic, Reliable, fast and flexible Logging Framework.
  • Since then, the Java Logging world has been divided into two camps: Commons Logging and SLF4J.

Commons Logging has a large user base under the Apache tree. But there is evidence that patterns are changing. At the end of 2013, someone analyzed 30,000 projects on GitHub and counted the 100 most popular Libraries, which shows that SLF4J has a better development trend

  • In July 2012 Apache rewrote log4j 1.x to create a new project Log4j2. Log4j2 has all the features of Logback.

SLF4J + Logback is now the default logging framework for Spring Boot

You can imagine the popularity

Classification of logging frameworks

Logging facade framework and logging implementation framework have been mentioned in the introduction above. What are logging facade framework and logging implementation framework

  • Logging facade frameworks refer to abstract facade frameworks with no concrete implementation, such as Commons Logging and SLF4J
  • Logging implementation framework: Implementation framework such as Log4j, Log4j2, Logback, Jul(java.util.logging)

Log facade frame

Log facade: is a typical application of facade pattern, also known as facade pattern. The logging facade framework is a set of frameworks that provide an interface to logging functionality without a concrete implementation, invoking a concrete implementation framework for logging. That is, the logging facade is naturally compatible with the logging implementation framework.

There are two main Logging facade frameworks in Java Ecology China: Commons Logging and SLF4J

Advantages of log facade frame

Log facade framework is a bridge between the specific log framework and the system. Through the application of the log facade framework, the decoupling of the system and the specific implementation of the log framework is realized. No matter how the log framework is changed, the logging function of the system is not affected, and the system code does not need to be changed, which complies with the open-close principle.

Commons Logging

Apache Commons Logging formerly known as JCL(Jakarta Commons Logging) is a Java-based Logging facade framework.

SLF4J

SLF4J(Simple Logging Facade for Java) is an interface program that wraps the Logging framework and is implemented in a Facade mode. You can decide which Logging framework to use during software deployment. Currently, Java Logging API, Log4j, logBack and other frameworks are supported. Distributed under an MIT license. SLF4J is written by Ceki Gulcu, the author of Log4j and Logback.

Commons Logging and SLF4J implementation mechanisms

Commons Logging implementation mechanism

Commons Logging uses its own ClassLoader to find and load locally specific implementations through a dynamic lookup mechanism while the program is running. Detailed strategy can see Commons logging – *. Jar package the org.apache.com mons. Logging. Impl. LogFactoryImpl. Java file. Because different OSGi plug-ins use separate Classloaders, OSGi’s mechanism ensures that plug-ins are independent of each other and limits the normal use of Commons logging in OSGi.

Slf4j implementation mechanism

Slf4j statically binds the local LOG library at compile time, so it can be used normally in OSGi. It is by looking for classpath org. Slf4j. Impl. StaticLoggerBinder, then the binding work is conducted in this class.

Logging Implementation framework

Log4j

The earliest logging implementation framework, originally developed by Ceki Gulcu, is now a project of the Apache Software Foundation.

JUL(Java Util Logging)

The official logging implementation since Java 1.4, mainly referenced from Log4j

Logback

A next-generation logging framework developed by Ceki Gulcu, an early author of Log4j

Log4j2

Apache Log4j2 is an upgrade of Apache Log4j and is incompatible with Log4j

Framework to choose

  • Cost considerations: All documentation for Logback is available free of charge, while Apache Log4J only provides part of the documentation free of charge.
  • Resource overhead: Commons Logging is more expensive than SLF4J.
  • Performance: Logback has better performance than Log4j and Log4j2. Logback claims that some key operations, such as deciding whether to log a statement, have significantly improved performance. This operation takes 3 nanoseconds in Logback and 30 nanoseconds in Log4J. LogBack also creates the logger faster: 13 milliseconds compared to 23 milliseconds in Log4J. More importantly, it takes 94 nanoseconds to fetch an existing logger, compared to 2234 nanoseconds for Log4J, which is 1/23 of the time. The performance improvement over JUL is also significant.

In addition to Spring, SLF4J + Logback is selected as the default logging framework for Spring Boot

SLF4J + Logback is therefore the best choice for actual development

Afterword.

Java logging implementation framework Logback