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

How does Spring Boot implement logging?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how Spring Boot implements logging. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Facade mode

When it comes to the logging framework, we have to say facade mode. The core of facade mode is that the external communication with a subsystem must be carried out through a unified appearance object, which makes the subsystem easier to use.

A picture is used to show the structure of the facade pattern as follows:

Facade mode

To put it simply, the pattern is to encapsulate some complex processes into an interface for easier use by external users.

In this model, three roles are involved:

Facade role: the core of the appearance model. It is called by the customer role and is familiar with the functions of the subsystem. Several functional combinations (modules) are booked internally according to the needs of the customer role.

Subsystem (module) role: realizes the function of the subsystem. It is unknown to the customer role and Facade. Within it, there can be interaction within the system, or there can be interfaces for external calls.

Customer role: complete the function to be implemented by calling Facede.

The log framework on the market

To put it simply, the log facade in the above table corresponds to the Facede objects in the facade mode, which are only an interface layer and do not provide logging implementation.

The log implementation corresponds to each subsystem or module, and the specific logical implementation of logging is written in the framework on the right; then our application is equivalent to the client.

Why use facade mode?

Imagine the scenario where we need to use a lot of packages to develop our system, and these packages have their own logging framework, so there will be a situation like this: our own system uses Logback as a logging system, and our system uses Hibernate.

The logging system used in Hibernate is jboss-logging, and our system uses commons-logging as the logging system used in Spring,Spring.

In this way, our system has to support and maintain the three logging frameworks of Logback, jboss-logging and commons-logging at the same time, which is very inconvenient.

The way to solve this problem is to introduce an interface layer, which decides which log system to use, and the caller only needs to print the log without caring about how to print the log. The log facade of the above table is this kind of interface layer.

In view of this, when we select a log, we must choose a framework from the log facade on the left and the log implementation on the right of the above table.

By default, SLF4j and Logback are selected at the bottom of Spring Boot to achieve log output.

SLF4j usage

Official documents give an example of this:

Import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloWorld {public static void main (String [] args) {/ / HelloWorld.class is the log of the specified class you want to print, / / if you want to print in other classes, just replace HelloWorld.class with the target class name .class. Logger logger = LoggerFactory.getLogger (HelloWorld.class); logger.info ("HelloWorld");}}

To understand how SLF4j works, I looked through its official documentation and saw a picture like this:

SLF4J

To explain briefly, there are six uses of SLF4J above, with a total of five roles. Application is, needless to say, our system.

SLF4J API is the log interface layer (facade); the blue and bottom gray is the specific log implementation (subsystem); and Adaptation is the adaptation layer.

Explain the second and third usage of the picture above. The second is the default usage of Spring Boot; why is there a third?

Because Log4J appeared early, it had no idea that there would be a SLF4J behind it. Log4J cannot be directly implemented as a log of SLF4J, so there is an adaptation layer in the middle. The fourth is the same.

As a reminder here, each log implementation framework has its own configuration file. After using SLF4J, the * * configuration file is still made into the configuration file of the logging implementation framework itself. For example, Logback uses logback.xml and Log4J uses Log4j.xml files.

How to unify all the logs in the system to SLF4J?

I continued to browse the official website and saw this picture:

Legacy

As you can see from the figure above, the way to unify all logs in the system to SLF4J is as follows:

Exclude other logging frameworks in the system first.

Replace the original logging framework with a tundish.

We import other implementations of SLF4J.

Log relationship in Spring Boot

Spring Boot implements logging using the following dependencies:

Org.springframework.boot spring-boot-starter-logging 2.1.3.RELEASE compile

Spring-boot-starter-logging has a diagram like this:

Log underlying dependency

As can be seen from the image above:

The underlying layer of Spring Boot2.x also uses slf4j+logback or Log4J for logging.

Spring Boot introduces an intermediate replacement package to replace all other logs with SLF4J.

If we want to introduce other frameworks, we can remove the default logging dependencies for this framework.

For example, Spring uses the commons-logging framework, and we can remove it like this:

Org.springframework spring-core commons-logging commons-logging

Spring Boot can automatically adapt to all logs, and the underlying use of slf4j+logback logging, when introducing other frameworks, you only need to exclude the logging framework that this framework depends on.

Log usage

① is configured by default (take Log4J framework as an example). Spring Boot helps us configure logs by default:

/ / Recorder Logger logger = LoggerFactory.getLogger (getClass ()); @ Test public void contextLoads () {/ / Log level; / / from low to high trace, you can specify that a certain configuration only takes effect in a certain environment.

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

Development

Wechat

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

12
Report