Hello everyone, I wrote two other types of articles before (shui), I feel that the response is not very good, so I obediently come back to update the core technology article.

After the first two articles in this series, we learned that the war of logging frameworks has ended with the dominance of SLF4j. However, SLF4j is only an interface, and logback and Log4j2 are still in a tight race in terms of implementation. Whether the logging framework implementation should choose Log4j2 or Logback. In this article, we will discuss functions, API design, scalability and performance.

ecological

The old Log4j2 with early entry, back to Apache two advantages have a good user support, official website documentation is also very perfect.

The new Logback, with its native implementation of SLF4j and the logging framework selected by Spring Boot (Spring also provides a Log4j2 starter to replace the logging framework by switching dependencies, as discussed above), also has a promising future.

In terms of community support, Log4j2 is a top-level project of Apache and Logback is a product of Ceki.

function

Logging functions can be divided into configuration, usage, and unique features from a user’s perspective.

  • In terms of configuration files, Log4j provides more configuration file configuration methods. Log4j2 supports properties, YAML, JSON, and XML, while Logback supports XML and Groovy.
  • Log4j2 provides an Appender for almost all scenarios, including files, console, JDBC, NoSql, MQ, Syslog, Socket, SMTP, etc. Logback provides an Appender slightly less than Log4j2, which provides files, console, database, Syslog, Socket, SMTP, etc. For dynamic output, Log4j2 provides ScriptAppenderSelector. Logback provides Evaluator and SiftingAppender (both of which can be used to judge and select the corresponding Appender);
  • The Logback module supports Socket Appender output from other Logbacks. Logbak also has a Logback-access module. Can be used to integrate with Servlet containers such as Tomcat and Jetty to provide HTTP access logging capabilities; Log4j2 has garbage-free (Garbage free mode) that claims to reduce JVM Garbage collection pauses, and the Log4j2 API supports using Java 8 lambda, SLF4j provides the Fluent API in version 2.0 and supports lambda.

API design and scalability

As mentioned earlier, SLF4j provides the Fluent API in version 2.0, where Logback will be implemented natively (theoretically better than dynamic translation used to be), while Log4j2 does not provide support. Extension, Logback using the configuration file directly write the corresponding implementation (class = “ch. Qos. Logback. Core. Rolling. RollingFileAppender”) from the definition to realize extension, Log4j2 using plug-in mechanism, no configuration, but is more complex, Personally, Logback is a little clearer.

performance

Log4j2 官网 has a very good performance test report, the result is that Log4j2 wins, in a serious attitude, the outside think also want to test their own, using JMH for synchronous and asynchronous output file test, test one minute, no preheat, using file scrolling mode, gzip compression, # logback 1.2.3, # log4j 2.13.0, # SLf4j 1.7.30, # JMH version In 1.22 # JDK 1.8.0_232, SLF4j was used as the API to output logs.

Log output format:

  • Logback: % D {YYYY-MM-DD HH: MM :ss.SSS} %5p [%t] %-40.40 Logger {39} : %m%n

  • Log4j2: % d {MM – dd yyyy – HH: MM: ss. The SSS} % 5 p – 40.40 – c [t] % % {1} : % m % n

    The test results are as follows:

From the test results, there is not much difference between the two. In terms of throughput performance, the synchronous output is better than the asynchronous output on the single thread, and the asynchronous output is better than Log4j2. In terms of response time, there is no significant difference between synchronous and asynchronous, and the more threads there are, the slower the response will be, which should be caused by the overhead of thread switching and locking. It is worth mentioning that the CPU usage will be higher in asynchronous output.

conclusion

Logback is simpler to use, Log4j2 is more powerful, the two are not much different if not used in depth, and can be switched seamlessly when using SLF4j. Personal advice, do not tangle with the selection, according to the preference of choice. The test source has been uploaded to Github, and in the next article we will explain the principle and how to extend the dynamic tuning log output level.

Java programmers are “lucky” that they do not have to do technology selection: junior programmers are unable to do technology selection, intermediate programmers often use already selected technologies, and senior programmers have to choose cTOS or customer-specified technologies.

The above is a personal point of view, if there is a question or error, welcome to leave a message to discuss correction, code word is not easy, if you feel good, for attention, for praise, for forwarding.

Scan the code to pay attention to the public number, the first time to get updated

Code: github.com/jiyuanwai/l…