Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the logging framework in the server?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of what the log framework in the server is, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading what the log framework in this server is. Let's take a look.

The story begins.

In an IT company, project manager Hu Li (dragon) is directing programmer Lu Xiaoming (elite dragon) to develop a large-scale add, delete, change and search project. In order to develop this project. Lu Xiaoming, the only programmer on the project team, works 996 every day.

The story enters the V1.0 segment.

One day, Tiger vigorously put forward a new requirement for Lu Xiaoming. In order to better carry out the company's information construction, Tiger wants to see the execution of the code. When executing a certain business, it is expressed in the console. For example, when the query method is executed, it needs to appear on the console. This is the information of a printing method.

Lu Xiaoming thought that it was easy to do this. Originally, he had written a lot of print statements for the convenience of debugging and testing, but now there are just more. So I worked overtime and wrote the System.out.println () print statement in all the add, delete, change and search methods. The work was successfully completed.

The story enters the V2.0 segment.

For a while, the years are quiet.

One day, Tiger vigorously found Lu Xiaoming: there are too many System.out.println () in your code. I need you to make it and show it during testing, but not after it is online. You go get it.

Lu Xiaoming pondered: should I comment out the printed statement when I go online and open it during the test?

But it is not easy to think of often switching comments on and off, so Lu Xiaoming gritted his teeth and changed 996 to 007, forgetting to eat and sleep and updated the V2.0 version. He encapsulates the log printing into a frame logging-1.0.jar, which can be uniformly switched. The work was successfully completed.

The story enters the V3.0 link.

For a while, the years are quiet.

One day, Hu vigorously found Lu Xiaoming: your log framework function is too simple, and then do some new functions, such as output to files, asynchronous ah.

Lu Xiaoming then pondered, continued 007 after 007, forgot to eat and sleep updated the V3.0 version, packaged into a new framework logging-2.0.jar. The work was successfully completed.

The story enters the v4.0 segment.

For a while, the years are quiet.

One day, Tiger vigorously find Lu Xiaoming: 1.0 and 2.0 api is not the same, 1.0 for 2.0, 2.0 for 1.0 to change the code every time you switch, you can change it to which one you want to use.

Lu Xiaoming then pondered that it was a bit difficult to continue 007 after 007. He found inspiration from JDBC. JDBC realized the driver switch through a unified interface, and the log was also available.

So he created a log interface layer (log facade) and let both 1.0 and 2.0 log frameworks implement this interface, so import 1.0 when you want to use 1.0 and 2.0 when you want to use 2.0. The task was successfully completed.

And the structure of this design is also the structure of the mainstream log framework: log4j logback log4j2 and so on.

Log facade (interface) Log implements SLF4J,commons-loggingLogback,Log4j

Through them, we print out all kinds of log information that we often see.

Analysis of log frame structure

The logging framework is actually divided into three parts, in addition to the log facade (interface) and log library (implementation) mentioned above, there is also a log adapter.

Log facade interface specification

The definition of the interface specification is not responsible for the specific implementation, that is, the method of the log facade interface that is called when the log is printed in the later code. It is common to have SLF4J,commons-logging. The common log facades are as follows

Brief introduction of log facade (interface specification layer) JCL (Jakarta Commons Logging) this jar is a common commons-logging.jar and is also a log facade used in the Spring framework. Since the last update was in 2014, it is not recommended to use SLF4j (Simple Logging Facade for Java), which is arguably the most commonly used log jar package. Jboss-logging is the least used, and some specific frameworks are in use.

According to a simple analysis, if we want to choose a log interface specification in our code, there is no doubt that only SLF4j is worthy of our project.

Log library code implementation

Log library is the concrete implementation of log function, which appeared in the early days to replace System.out statements. The commonly used log libraries are as follows:

A brief introduction to the log library (log implementation) log4j was born earliest and used most logback the latest. It is the same author as log4j and is the upgraded version of log4j log-jdkjdk. Java.util.logging is abbreviated to log-jdk in version 1.4.

In actual development, log4j and logback are widely used, but if you are developing a new project, logback is recommended.

Log facade adapter also known as interface converter

What is this thing? To start with history, in the history of the log framework, there is not first a log facade (interface specification), followed by a log library. The reality is that it took a while for the author to write log4j before he had slf4j.

Tips:

Slf4j log4j logback is the same father (author)

Log4j can be equivalent to 1.0 written by Lu Xiaoming at that time, there was no interface specification.

Because of this sequence problem, there is a very awkward situation, the code implementation is not compatible with the interface, because the log4j code does not implement the slf4j interface, so to use slf4j+log4j, there needs to be an intermediate layer (log facade adapter) to solve the interface compatibility problem.

For example: what if the ultrabook you bought doesn't have an Internet cable Jack? Buy a USB converter, which is what we call the middle layer, that is, the log facade adapter. Slf4j+ "Converter" + log4j will work properly.

Through this design pattern, slf4j is compatible not only with log4j, but also with many other logging frameworks. You can see such a picture by reading the official slf4j documentation.

1 / 2 / 3 / 4 is marked in the picture. Explain it against the previous concept.

Tag 1: this is a case where there is only interface specification and no log implementation, and there will be no log information output if only the interface is not implemented in the project.

Tag 2: logback is used as the log library in the application code. You can see that logback directly implements the SLF4J interface and does not need a "converter". Interface layer + code implementation. This is one of the differences between logback and log4j.

Tag 3: the application code uses log4j as the log library implementation, which is shown after slf4j-api.jar, followed by a slf4j-log4j12.jar, which is the "converter" log facade adapter we mentioned. The relationship between interface layer + converter + code implementation.

Tag 4: there is also an adapter jar package in this illustration, but this jar package is for log-jdk compatibility.

So the question is, what's the difference between logback and log4j in use?

Log database adapter, also known as slf4j, how to unify the log world?

The log facade adapter "interface implementation adapter" solves the problem of incompatibility between interface specification and implementation. We can finally happily start printing boring journals.

But wait, don't you think there's something wrong? You taste, you taste fine.

Although our project uses slf4j to print logs happily, other frameworks do not use slf4j, such as Spring (commons-logging) and Hibernate (jboss-logging). In this case, our project is like a country with many sets of laws, so who does it listen to, so we need to let other frameworks and log4j use slf4j to output logs?

How can you do that? This is going to use the log library adapter. It's also an adapter. Give me a chestnut.

Slf4j is the equivalent of an Apple computer with only a type-c interface. Now you have a lot of USB drives that you want to connect to your computer, but the USB flash drive uses the USB interface. What should I do?

Just buy a converter from type-c to USB.

Find the following figure through the official documentation-shows how other logging frameworks are connected to slf4j

There are three diagrams on this picture. Since the principles of the three are all the same, let's take the one in the upper left corner as an example to explain it briefly.

This picture we first look at vertically, that is, the application uses logback as a logging framework, interface specification + implementation of the simple relationship.

Then we look sideways and we will see commons-logging log4j (said earlier that log4j was born early) log-jdk. These are the logging frameworks used by other frameworks in the application, such as Spring. What should we do if we want to connect these frameworks to slf4j?

Take commons-logging as an example. If you use Spring,Spring in your project, you need to use commons-logging.jar. How do you make the Spring log output via slf4j?

1. We need to rule out commons-logging.jar first, after all, it does not output to slf4j function, but after exclusion, spring will report an error, ah, do not panic this is the second step.

two。 Replace commons-logging.jar with jck-over-slf4j.jar. What is the purpose of this package? It is the converter we are looking for. On the one hand, it has the same function as commons-logging, which can be replaced completely; on the other hand, it can be seamlessly connected to slf4j.

The intermediate converter package is the log library adapter.

Simply, you can unify commons-logging to slf4j through the above two steps.

Through this logic, we can simply unify log4j,log-jdk to slf4j through the intermediate conversion package, and then realize the unified log of slf4j.

This is the end of the article on "what is the logging framework in the server?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the log framework in the server". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report