XML documents have a hierarchical structure and can conceptually be interpreted as a tree structure. Return to the start method. Take a look at the doConfigure method:. You can see that, this method parses the previously obtained configuration, and inserts the result to the right place. After completing the configuration, you need to start the logger and appender. AsyncAppender is an outstanding feature that differentiates Log4j 2 from other logging services.
follow url We will start with log printing to see how it works. Go to the Logger, and find a logging method. As you can see, the number calls is recorded before log printing. Take a look at the tryLogMessage method:. As you can see, upon log printing, the log printing strategy is retrieved from config. Take a look at how the ReliabilityStrategy is created. The default implementation class is DefaultReliabilityStrategy.
Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available. Apache Log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is part of the Apache Logging Services project of the Apache Software Foundation. Log4j is one of several Java logging frameworks.
Take a look at the actual log printing method:. The actual log printing method is implemented by a config. It is a quite unusual. Take a closer look:.
You can clearly see that the snippet before try creates the LogEvent, and only the try part implements logging. Keep moving. Next, we will take a look at the callAppenders method. First, start with the append method of AsyncAppender:. Create the LogEvent 2. Add the LogEvent to the BlockingQueue by calling the transfer method 3.
By; import org. Common Options edit. TimeUnit; import org. Getting Help edit. Log4j is a fast, flexible and reliable logging framework APIS written in Java developed in early
If the BlockingQueue is full, enable the corresponding strategy. Block the thread to obtain the LogEvent 2. Send out the LogEvent 3. A thread cannot be shut down until all events of the blocking queue are consumed. The logic of this method is straight forward. RingBuffer can be considered a lock-free queue of Disruptor. We can assume that the log information will be processed by some consumption logic after being published to the RingBuffer. We can find this consumption logic using one of two ways:.
From the comments, we can find that the onEvent method is the processing logic. To achieve better extensibility, many Log4j 2 components are made in plug-ins. You need to load plug-in-based components during configuration. Load all built-in plug-ins from Log4j2Plugin. Load all plug-ins from Log4j2Plugin.
Load plug-ins that have been passed to the package path 4. At last, load plug-ins from the configuration. The logic is straight forward.
However, when I check the source code, I found something interesting. When we load Log4j 2 core plug-ins, the following method is used:. I was wondering why it loads plug-ins from files instead of directly scanning them through reflection? When plug-ins are written into files, they are not easily scalable. I found the PluginProcessor class, which uses an annotation processor. You can see that in the process method, the PluginProcessor class first collects all plug-ins, and then writes them into files. By doing so, it saves the reflection overhead. Take a look at the comment for plug-ins.
Generally, PluginProcessor is used in conjunction with RetentionPolicy. This is something that I do not quite understand. At last, we have gone through the code of Log4j 2. Its design ideas are inspiring - developing components into plug-ins improves the flexibility. With the rapid development of Internet technologies, various middleware products have been springing up. We need to think more about the relationship between one code and another.
Undoubtedly, the decoupled relationship is the most beautiful one. Introduction to the Spring Cloud Stream System. An online computing service that offers elastic and secure virtual cloud servers to cater all your cloud hosting needs. More Posts by Alibaba Clouder. Exploring the Secrets of Java Logs: Log4j 2 Log System This article dissects the source code of Log4j 2, a new log system built on Apache Log4j, to get a deeper understanding of its underlying concepts.
Overview Log4j 2 is a completely new log system built on Apache Log4j by referencing the logback architecture. For advantages of Log4j 2, see the official document Apache Log4j 2 : Log4j 2 provides many of the improvements available in Logback while fixing some inherent problems in the Logback architecture. In multi-threaded scenarios Asynchronous Loggers have 18 times higher throughput and orders of magnitude lower latency than Log4j and Logback. Avoid lock-in: Applications coded to the Log4j 2 API always have the option to use any SLF4J-compliant library as their logger implementation with the log4j-to-slf4j adapter.
Automatic reloading of configurations: Like Logback, Log4j 2 can automatically reload its configuration upon modification. Unlike Logback, it will do so without losing log events while reconfiguration is taking place. Advanced filtering: Like Logback, Log4j 2 supports filtering based on context data, markers, regular expressions, and other components in the Log event.
Plugin architecture: Log4j uses the plug-in pattern to configure components. As such, you do not need to write code to create and configure an Appender, Layout, Pattern Converter, and so on. Log4j automatically recognizes plug-ins and uses them when a configuration references them. Property support: You can reference properties in a configuration, Log4j will directly replace them, or Log4j will pass them to an underlying component that will dynamically resolve them.
Java 8 Lambda support Custom log levels Garbage-free: During steady state logging, Log4j 2 is garbage-free in stand-alone applications, and low garbage in web applications. This reduces pressure on the garbage collector and can give better response time performance. Integrating with application servers: Version 2. Log4j 2 classes diagram: This article explores the source code of Log4j 2 in four aspects: startup, configuration, asynchronization, and plug-in-based components.
Using SimpleLogger to log to the console UrlResource resource : LoaderUtil. Differences between lockInterruptibly and lock are: Differences between lock and lockInterruptibly are: lock first acquires the lock, and reacts to the interrupt signal only after acquiring the lock. This is primarily used for OSGi support. Return to the LogManager.
While quite a few legacy projects still use it, you should prefer one of the other frameworks discussed in this article if you start a new project. If you want to use Log4j in your application, you need to add the log4j.
You can see the required Maven dependency in the following code snippet. You also need to add the following dependency to be able to use Log4j via the standardized interfaces. In addition to the log4j.
The appender writes the log messages to a destination such as a file or database. The logger and level define the granularity of log messages that are written to the log file. The following code snippet shows a typical Log4j configuration for a development system of an application that uses Hibernate as an object-relational mapper. It writes all log message to the file app. The configuration also sets the log levels of the logger org. Logback was written by the same developer who implemented Log4j with the goal to become its successor.
It follows the same concepts as Log4j but was rewritten to improve the performance, to support SLF4J natively, and to implement several other improvements like advanced filtering options and automatic reloading of logging configurations. Logback-core provides the core functionality of the logging framework. Logback-classic adds more features to the core functionality, e. And logback-access integrates it with servlet containers so that you can use it to write HTTP-access logs. You only need to define a dependency on logback-classic.
You can change that with a custom configuration file in XML or Groovy format. Logback uses the same concepts as Log4j.