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

Operation method of Spring Boot and Log SLF4J

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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

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

Internet Technology

Wechat

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

12
Report