Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Operation method of Spring Boot and Log SLF4J

Shulou Source: shulou.com Published: 2022-06-02 03:51:38 09月28日 Update

The operation method of Spring Boot and Log SLF4J, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1. Log framework

Log framework on the market:

Log facade (abstract layer of log) Log implements JCL (Jakarta Commons Logging) SLF4J (Simple Logging Facade for Java) Jboss-loggingLog4j JUL (java.util.logging) Log4j2 Logback

Choose a facade on the left and an implementation on the right (bold is written by the same person, Logback is newer than Log4j)

Log facade: SLF4J

Log implementation: Logback

Spring Boot: the bottom layer uses the Spring framework, and the Spring framework uses JCL by default

SLF4J and Logback are selected for Spring Boot

2. The use of SLF4J. How to use SLF4J in a system

When developing later, the call of the log method should not directly call the implementation class of the log, but should call the method of the log abstraction layer.

Slf4j's jar package and logback's implementation jar package should be imported into the system.

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class HelloWorld {public static void main (String [] args) {Logger logger = LoggerFactory.getLogger (HelloWorld.class); logger.info ("HelloWorld!");}} public static Logger getLogger (String name) {ILoggerFactory iLoggerFactory = getILoggerFactory (); return iLoggerFactory.getLogger (name);} public static Logger getLogger (Class clazz) {Logger logger = getLogger (clazz.getName ()) If (DETECT_LOGGER_NAME_MISMATCH) {Class autoComputedCallingClass = Util.getCallingClass (); if (autoComputedCallingClass! = null & & nonMatchingClasses (clazz, autoComputedCallingClass)) {Util.report (String.format ("Detected logger name mismatch. Given name:\ "% s\"; computed name:\ "% s\"., logger.getName (), autoComputedCallingClass.getName ()); Util.report ("See http://www.slf4j.org/codes.html#loggerNameMismatch for an explanation");}} return logger;}

The figure shows:

Each log framework has its own configuration file format. After using slf4j, you still need to configure it using the configuration files of each log implementation framework.

two。 Remaining problems

Many frameworks are used in the same project, and the log implementation used by each framework is bound to be different. How to use slf4j + logback to log output uniformly?

Exclude other log frameworks in the system first.

Replace the original logging framework with tundish

Import other implementations of slf4j

Slf4j "title=" log4j-- > slf4j ">

3. Log relationship of SpringBoot

Summary:

The bottom layer of spring boot is also implemented by slf4j + logback.

Spring boot replaced all the other logs with slf4j

Intermediate replacement package

Example of conversion:

If we are going to introduce other frameworks, we must eliminate the original framework.

4. The log uses 1. Default configuration

By default, SpringBoot has configured the log module for us.

How to call SpringBoot logging:

/ / Recorder Logger logger = LoggerFactory.getLogger (SpringBoot03LoggingApplication.class); @ Testpublic void contextLoads () {/ / System.out.println (""); / / the level of the log, from low to high trace

Tags: Log framework configuration level file method path this is format problem build output way more feature system facade advanced underlying project Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Shulou Tech Info Xiaomi Microsoft