In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the knowledge points of the Java log system". In the daily operation, I believe that many people have doubts about the knowledge points of the Java log system. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the knowledge points of the Java log system?" Next, please follow the editor to study!
I. Classification of the log framework
Facade log framework:
JCL: the project of the Apache Foundation is a set of Java logging interfaces, formerly called Jakarta Commons Logging, and later renamed Commons Logging
SLF4J: is a set of simple Java log facade, there is no log implementation. (Simple Logging Facade for Java, abbreviated Slf4j)
Recording log framework:
JUL: the logging tool in JDK, also known as JDKLog, jdk-logging, is the official logging implementation since Java1.4.
Log4j: a specific logging implementation framework.
Log4j2: a specific logging implementation framework, which is the next version of LOG4J1, has changed a lot from Log4j 1. Log4j2 is not compatible with Log4j 1.
Logback: a specific logging implementation framework, which is the same author as Slf4j, but has better performance.
II. The course of development
To understand their relationship, it is necessary to start with the circumstances under which they came into being. Let's introduce it in chronological order.
Log4j
Before JDK 1.3, Java logging relied on System.out.println (), System.err.println (), or e.printStackTrace (), Debug logs were written to STDOUT streams, and error logs were written to STDERR streams. There is a very big defect in logging in this way, that is, there is no legalization, and the log granularity is not fine enough.
As a result, G ü lc ü launched Log4j in 2001 and later became a top-level project of the Apache Foundation. Log4j is excellent in design and has a long and far-reaching impact on the follow-up Java Log framework. The concepts defined by Logger, Appender, Level and so on are now widely used. The weakness of Log4j is performance, and with the advent of Logback and Log4j2, the use of Log4j has been reduced.
J.U.L
Inspired by Logj, Sun introduced java.util.logging in the Java1.4 version, but j.u.l is far less functional than log4j. Developers need to write their own Appenders (Sun calls it Handlers), and only two Handlers are available (Console and File). The performance and availability of j.u.l have been improved after Java1.5.
JCL (commons-logging)
Since the log printing of the project is bound to choose at least one of the two frameworks, Apache's JCL (commons-logging) is born. JCL is a Log Facade that provides only Log API and no implementation, and then there is Adapter to use Log4j or JUL as Log Implementation.
Log creation and recording in the program are using the interface in JCL, in the real run, will see what is implemented in the current ClassPath, if there is Log4j is to use Log4j, if there is nothing is to use JDK's JUL.
In this way, in your project, as well as in third-party projects, everyone logs using the JCL interface, and then when you finally run the program, you can choose to use the appropriate Log Implementation according to your own needs (or preferences). If you use Log4j, add the jar package for Log4j and write a configuration file for Log4j; if you like to use JUL, just write a configuration file for JUL. If any other new log library appears, you only need it to provide an Adapter, and the runtime adds the jar package for this log library.
However, commons-logging is not compatible with Log4j and j.u.l configuration issues, and using commons-loggings may also encounter class loading problems, resulting in NoClassDefFoundError errors.
By this time, everything seemed simple and beautiful. The interface and implementation are well separated. Under a unified JCL, without changing any code, you can switch to a more powerful or better performance log library through configuration.
This simple beauty continued until the advent of SLF4J.
SLF4J & Logback
SLF4J (Simple Logging Facade for Java) and Logback are also projects created by G ü lc ü to provide higher performance implementations.
From a design pattern perspective, SLF4J is used to play a facade between log and the code layer, similar to JCL's Log Facade. For users, as long as you use the interface provided by SLF4J, you can hide the specific implementation of the log. The core API provided by SLF4J is some interfaces and a factory class of LoggerFactory. Users only need to follow the unified logging interface it provides. The format, recording level and output mode of the final log can be realized through the configuration of the specific log system, so you can switch the log system flexibly.
Logback is an upgraded version of log4j and is currently divided into three target modules:
Logback-core: the core module, which is the basic module of the other two modules
Logback-classic: an improved version of log4j, and the full implementation of SLF4J API allows you to easily replace it with other diary systems such as log4j or JDK14 Logging
Logback-access: the integration of access module and Servlet container provides the function of accessing diary through Http, which is an indispensable part of logback.
Logback has more advantages than log4j:
Faster execution
More adequate testing
Logback-classic implements SLF4J very naturally.
Use XML configuration files or Groovy
Automatically reload the configuration file
Gracefully recover from the IWeiO error
Automatically clear old log archives
Automatically compress archive log files
Cautious mode
Lilith
Conditional processing in configuration file
Richer filtering
For a more detailed explanation, see the official website: https://logback.qos.ch/reasonsToSwitch.html
At this point, you may ask: Apache already has a JCL that can be used as a unified interface for all kinds of Log lib. If G ü lc ü wants to make a better Log implementation, just write an implementation, why do you want to do an and SLF4J?
The reason is that G ü lc ü thinks that JCL's API is not well designed, which makes it easy for users to write code with performance problems. You can refer to this article for a more detailed introduction to this point: https://zhuanlan.zhihu.com/p/24272450
Now things are getting complicated. We have two popular Log Facade and three popular Log Implementation. G ü lc ü is a perfectionist who decided that these Log can be easily replaced with each other, so he made various Adapter and Bridge to connect:
You can see that even Log4j and JUL can bridge to SLF4J and then adapt to Logback through SLF4J! It should be noted that there can be no cyclic bridging, for example, the following dependencies cannot exist at the same time:
Jcl-over-slf4j and slf4j-jcl
Log4j-over-slf4j and slf4j-log4j12
Jul-to-slf4j and slf4j-jdk14
However, things are getting more troublesome!
Log4j2
Now that there are better SLF4J and Logback, slowly replacing JCL and Log4j, it's time for the great unity to come to a successful end. However, Log4j maintainers don't think so. they don't want to sit back and watch users get eaten up by SLF4J / Logback and come up with Log4j2.
Log4j2 and Log4j1.x are not compatible, the design imitates SLF4J/Logback to a large extent, and the performance has been greatly improved. Log4j2 has also done the design of Facade/Implementation separation, divided into log4j-api and log4j-core.
Now that we have three popular Log interfaces and four popular Log implementations, what would it look like to draw a diagram of bridging relationships?
At this point, the study of "what are the knowledge points of the Java log system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.