In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use the log4j2 logging framework in Spring Boot 2. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Exclude Logback dependencies
Spring Boot 2.x uses the Logback logging framework by default, and Logback must be excluded before using Log4j2.
Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging introduces Log4j2 dependency org.springframework.boot spring-boot-starter-log4j2
The above log4j2 has adapted to the slf4j log facade, so our code does not need to be replaced, only the specific logging framework and the corresponding configuration file need to be replaced.
Configure Log4j2
Create the log4j2.xml file and put it in the project resources directory. This allows you to add no configuration. If you need to specify the configuration file, you need to specify the logging.config attribute in the Spring boot configuration file application.yml. The following is a more detailed log4j2 configuration file:
Basically, you can take the above configuration and change it according to your own needs. Under windows, ${sys:user.home} will print the log to the user directory.
Call Logger output log
The following sample code uses the @ Slf4j annotation in the artifact lombok to make it easy to use the org.slf4j.Logger object. Daily developers try to use Slf4j facade to deal with logs and try to avoid using specific logging frameworks.
Package cn.felord.spring.security;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;import java.util.Properties / * * @ author dax * @ since, 2019-10-9 0:03 * / @ Slf4j@RestController@RequestMapping ("/ logging") public class LogController {@ GetMapping ("/ do") public String log () {log.info ("log4j2 test date: {} info: {}", LocalDate.now (), "Please follow the official account: Felordcn"); return "log4j2";}}
Console output: [INFO]-cn.felord.spring.security.LogController.log (LogController.java:23)-log4j2 test date: 2019-10-09 info: please follow the official account: Felordcn and file.
The above is how to use the log4j2 logging framework in Spring Boot 2. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.