Java Common Log Framework comparison and in-depth analysis

Source: Internet
Author: User
Tags log4j

Objective

As a veteran developer, the logging framework must not be unfamiliar. And in almost all applications, there will be a variety of

The log framework is used to record the running information of a program. And for a mature Java application, this is essential. In the development and debugging phase, the log can help us to locate the problem faster, and in the operation of the application process, the log system can help us to record most of the exception information, often many enterprises will collect log information to the system's operational status of real-time monitoring and warning. So, how much do you know about the log framework?

Common Log Frames

Log4j

Log4j is Apache's next very rich Java Log Library implementation, log4j should be the older and most popular Java log group

Is a Java-based, open-source log component. The log4j feature is very powerful and can be exported to the console, file, and user interface via log4j. You can also export to the operating system's event logger and some system resident processes. It is worth mentioning that log4j allows you to customize log formats and log levels very easily, helping developers to control their own log information in all directions.

Log4j2

LOG4J2 is an upgraded version of LOG4J1. Log4j2 basically reconstructed the core of the log4j version and made a lot of optimizations and changes based on log4j.

Logback

Logback is another open source log component designed by the LOG4J founder, and is also seen as a substitute for log4j. and the official building and

SLF4J use together, you must not know Logback, slf4j, log4j are all from the same person. Logback is an improved version on the basis of log4j, and SLF4J is designed by the same person, so the slf4j is seamlessly combined by default.

Jdk-logging

A self-brought log library implementation that is available after the Jdk1.4 version

Unified Logging Module

At present, there are two widely used unified log specification interface, respectively, is

Slf4j

SLF4J (Simple Logging facade for Java) is an API-based Java Log framework, SLF4J provides a single, unified day

Log interface, developers in the configuration and deployment, only need to implement this interface can implement the logging function. It can be said that it is not a specific log solution, it simply serves a wide variety of log systems, allowing end users to use their own common log system on deployed applications

Commons-logging

Common-logging provides a unified interface for many specific log implementation libraries, similar to slf4j, which allows arbitrary log libraries to be bound at runtime;

In fact, there is a small story, when Apache persuaded Log4j and other log frameworks in accordance with the commons-logging standards, but because of commons-logging class loading a bit of a problem, the implementation is not friendly. Therefore the author of the log4j created slf4j, and therefore with commons-logging two parts of the world

A diagram of the relationship between several log frames

Feature demos for each log

The function of each log module demonstration and configuration instructions, I do not say much, online search the next catch a lot, all speak very detailed.

Principles of integration of slf4j and individual log frameworks

One thing to highlight here is that SLF4J integrates the various log frameworks through a very interesting and very bull design.

If you go to see the source of the slf4j, there is a static global variable in the Loggerfactory.java

private static String Static_logger_binder_path = "Org/slf4j/impl/staticloggerbinder.class";

There's a core code.

Static Set Findpossiblestaticloggerbinderpathset () {Linkedhashset staticloggerbinderpathset = new LinkedHashSet (); try {ClassLoader IoE = LoggerFactory.class.getClassLoader (); enumeration paths; if (IoE = = null) {paths = Classloader.getsystemresources (Static_logger_binder_path);} else {paths = Ioe.getresources (STA Tic_logger_binder_path); } while (Paths.hasmoreelements ()) {URL path = (URL) paths.nextelement (); Staticloggerbinderpathset.add (path);}} catch (IOException var4) {util.report ("Error Getting resources from Path", VAR4);} return staticloggerbinderpathset; }

Everyone to the ClassLoader mechanism understanding of the classmate, this code looks very easy to understand, through the ClassLoader to load all the Staticloggerbinder.class files classpath. Find this file and add it to a collection later. By loading the staticloggerbinder into the corresponding jar. To get the instance.

public static Iloggerfactory Getiloggerfactory () {if (initialization_state = = uninitialized) {synchronized (loggerfacto Ry.class) {if (initialization_state = = uninitialized) {initialization_state = ongoing_initialization; Performinitialization (); }}} switch (initialization_state) {case Successful_initialization:return Staticloggerbinder.getsingleton (). Getloggerfactory (); Case Nop_fallback_initialization:return Nop_fallback_factory; Case Failed_initialization:throw New IllegalStateException (UNSUCCESSFUL_INIT_MSG); Case Ongoing_initialization://Support re-entrant behavior. See also http://jira.qos.ch/browse/SLF4J-97 return subst_factory; } throw new IllegalStateException ("Unreachable Code"); }

In this code, you can see that there is a Staticloggerbinder.getsingleton (). Getloggerfactory ()

This is the instance returned in the third-party integration pack. This is one of the more central slf4j inside.

Welcome to 1-5 years of Java programmer friends to join the Java Architecture development: 744677563

The Group provides free learning guidance structure materials and free answers

Do not know the problem can be raised in the group after the career planning and interview guidance

Group comments: Development years-region-experience

Facilitate the architect to answer questions

Java Common Log Framework comparison and in-depth analysis

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.