A common Java logging framework

  • log4j
  • logback
  • j.u.l (java.util.logging)

Common Java logging facade

  • SLF4J
  • commons-logging

Of these, J.U.L is a Java native library, but was only referenced in Java 1.4; Commons-logging comes from Apache and is used to bridge J.U.L and log4j; Log4j, LogBack, and SLF4J come from the same author (Ceki) and are widely used in open source projects. The time line of the above log frame and log facade is shown as follows:

This section briefly introduces each node according to the time sequence of the above frame and facade

log4j

Prior to JDK 1.3, Java logging relied on system.out.println (), system.err.println (), or e.prinintStackTrace (). Debug logs were written to the STDOUT stream, and error logs were written to the STDERR stream. One of the major disadvantages of logging this way is that it cannot be customized and the log granularity is not fine enough. Log4j was born out of this environment. It is a landmark framework that defines Logger, Appender, Level, and other concepts that are now widely used.

j.u.l

Inspired by Logj, Sun introduced Java.util. logging in Java1.4, but J.U.L is not nearly as good as Log4j, and developers need to write Appenders (Sun calls them Handlers). With only two Handlers available (Console and File), J.U.L did not improve in performance and usability until Java1.5.

commons-logging (Jakarta Commons Logging: j.c.l)

Since logging for a project must be done in at least one of the two frameworks, Apache’s Commons-logging was born. Commons-logging is not a logging printing framework, but an API Bridge that developers can use to work with different logging frameworks (log4J and J.U.L). However, commons-logging is not compatible with Log4j and J.U.L configuration issues, and it is possible to encounter classloading problems with commons-Loggings, resulting in NoClassDefFoundError errors.

SLF4J

Ultimately, Log4j founder Ceki initiated the SLF4J (Simple Logging Facade for Java) project, which, from a design pattern perspective, was designed to act as a Facade between the log and the code layer for users to simply use the interface provided by SLF4J, The core API provided by SLF4J is some interfaces and a Factory class of LoggerFactory. Users only need to follow the unified logging interface provided by SLF4J. The format, record level and output mode of the final log can be realized through the configuration of the specific logging system, so the log system can be flexibly switched.

  • Parameterized Logging
logger.error("the exception message is {}", message);Copy the code

logback

Logback is created by the same author as log4j. It is an updated version of log4j, so logback has more advantages than log4j:

  • SLF4J API natively implemented (log4j requires a mid-tier transformation)
  • XML and Groovy configurations are supported
  • Add criteria to configuration files
  • More powerful filters
  • Richer free documentation
  • More extensive testing
  • Automatically reloads the changed configuration file
  • Automatically compress historical logs
  • The pachage name and version are automatically included when the exception information is printed
  • Other features

    The logging framework is generally used in conjunction with the logging facade. See Reference 3 for commons-Logging (J.C.L). As SLF4J is widely used, the usage of SLF4J is mainly introduced below. There are two ways to use SLF4J, one is concrete- Bindings and the other is bridge-Legacy. The mixed binding mode means that SLF4J is used directly in the project to print logs, while the underlying binding is either of the logging frameworks, such as log4j, Logback, J.U.L, etc. Hybrid binding can be divided into two types based on implementation principles: adapter binding and no adapter binding. An adapter binding means that the underlying interface does not implement SLF4J, but calls the logger of the underlying logging framework through the adapter. No adapter binding means that the logger implements all the interfaces of SLF4J (Logback) and does not need to call the logger of other logging frameworks. Introduction to several hybrid bundles:

  • Slf4j-log4j12-1.7.21.jar: has an adapter, is bound to log4j, logger is provided by log4j-1.2.17.jar

  • Slf4j-jdk14-1.7.21.jar: with adapter, bound to J.U.L, logger provided by J.U.L
  • Logback-classy-1.0.13.jar: Without an adapter, logback implements all the interfaces of SLF4J
  • Slf4j-simply-1.7.21.jar: no adapter, simple implementation of SLf4J, only prints logs at the info level and above, all output is redirected to System. Err, suitable for small applications.

    Later, the concrete implementation of mixed binding with adapter (SLf4J + Log4j) and mixed binding without adapter (SLFJ + Logback) will be introduced respectively.