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 to use Log4j for SpringBoot

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use SpringBoot Log4j, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Brief introduction of log4j, logback, Log4j2

Log4j is an open source logging component implemented by apache, logback, also designed by the authors of log4j, has better features to replace a logging framework of log4j, is the native implementation of slf4j Log4j2 is an improved version of log4j 1.x and logback, and adopts some new technologies (lock-free asynchronous, etc.), which makes the throughput and performance of logs 10 times higher than that of log4j 1.x, and solves some deadlock bug, and the configuration is more simple and flexible.

The difference between slf4j+log4j and using log4j directly

Slf4j is a specification, standard, interface for all logging frameworks, and is not a specific implementation of a framework, because the interface cannot be used independently and needs to be used in conjunction with a specific logging framework implementation (such as log4j, logback). The advantage of using the interface is that when the project needs to change the logging framework, only the jar and configuration need to be changed, and there is no need to change the relevant java code.

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TestSlf4j {/ / Logger and LoggerFactory imported org.slf4j packages private final static Logger logger = LoggerFactory.getLogger (TestSlf4j.class);}

Log4j, logback and log4j2 are all specific implementation frameworks for logging, so they can be used alone or in combination with slf4j.

Import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class TestLog4j {/ / Logger and LogManager imported org.apache.logging packages private static final Logger LOG = LogManager.getLogger (TestLog4j.class);}

Import the jar package (slf4j+log4j2) that you need to use

Log4j2

If there is an import spring-boot-starter-web dependency package in the project, remember to remove the log dependency spring-boot-starter-logging that comes with spring

Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-logging

Log4j2 needs to be imported into springboot project.

Org.springframework.boot spring-boot-starter-log4j2

Log4j

If you want to use log4j, replace the coordinates of log4j2 with the one below, and still exclude the original spring-boot-starter-logging.

Org.springframework.boot spring-boot-starter-log4j 1.3.8.RELEASE

If you use log4j, you can create a new log4j.properties directly under resource.

Https://www.xuebuyuan.com/article/143488.htm

Configure XML location Log4j2

Springboot mode

Add configuration logging.config=classpath:log4j2_dev.xml,log4j2_dev.xml to application.properties is the configuration file name of the log4j2 you created. Put it under resources. If you put it in another path, modify it accordingly.

Web engineering mode

Log4jConfiguration / WEB-INF/conf/log4j2.xml org.apache.logging.log4j.web.Log4jServletContextListener

Java mode

Public static void main (String [] args) throws IOException {File file = new File ("D:/log4j2.xml"); BufferedInputStream in = new BufferedInputStream (new FileInputStream (file)); final ConfigurationSource source = new ConfigurationSource (in); Configurator.initialize (null, source); Logger logger = LogManager.getLogger ("myLogger");}

Format of configuration file: log2j configuration file can be in xml format or json format

Location of configuration files: log4j2 will look for files with names such as log4j2.xml, log4j.json, log4j.jsn and so on in the classpath directory by default. If no files are found, they will be output according to the default configuration, that is, output to the console. You can also customize the location of configuration files (you need to configure them in web.xml) and generally place them in the src/main/resources root directory.

The above is all the contents of the article "how SpringBoot uses Log4j". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Development

Wechat

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

12
Report