In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of the Spring Boot log framework", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of the Spring Boot log framework".
In Java applications, logs are generally divided into the following five levels:
ERROR error message
WARN warning message
INFO General Information
DEBUG debugging information
TRACE tracking information
Spring Boot uses Apache's Commons Logging as the internal logging framework, which is only a logging interface, which needs to be specified for the corresponding logging implementation in practical applications.
The default logging implementation of SpringBt is Java Util Logging, which is the log package that comes with JDK. Of course, SpringBt also supports popular logging implementations such as Log4J and Logback.
The above logging implementations are collectively referred to as the logging framework.
Let's put it into practice!
Use the Spring Boot Logging plug-in
First, add the configuration to the application.properties file:
Logging.level.root=INFO
The code of the controller is as follows:
Package com.hansonwang99.controller;import com.hansonwang99.K8sresctrlApplication;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping ("/ testlogging") public class LoggingTestController {private static Logger logger = LoggerFactory.getLogger (K8sresctrlApplication.class); @ GetMapping ("/ hello") public String hello () {logger.info ("test logging..."); return "hello" }}
Running result
Because the log level is set to INFO, log information containing INFO and above will be printed.
As you can see here, most of the INFO logs come from the SpringBt framework itself. If we want to block them, we can set the log level to ERROR first, so that the INFO information of the framework itself will not be printed. Then set the specific package in the application to the DEBUG-level log so that you can see only the DEBUG and above level logs in the package you are interested in.
Controls the log level of a specific package
Modification of configuration in application.yml
Logging: level: root: error com.hansonwang99.controller: debug
Obviously, set the root log level to ERROR, and then set the log level of the com.hansonwang99.controller package to DEBUG, that is, disable all and then allow individual setting methods
Controller code
Package com.hansonwang99.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping ("/ testlogging") public class LoggingTestController {private Logger logger = LoggerFactory.getLogger (this.getClass ()); @ GetMapping ("/ hello") public String hello () {logger.info ("test logging..."); return "hello";}}
Running result
It can be seen that the INFO level logs of the framework itself are all hidden, while the logs in the specified package are printed smoothly by level.
Output the log to a file
Logging: level: root: error com.hansonwang99.controller: debug file: ${user.home} / logs/hello.log
Running result
Using Spring Boot Logging, we found that although the log has been output to a file, a copy is still printed in the console, and it is found that using org.slf4j.Logger can not solve this problem.
Integrated Log4J logging framework
Add dependencies to pom.xml
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-logging Org.springframework.boot spring-boot-starter-log4j2
Add the log4j2.xml file in the resources directory as follows:
The other code remains the same.
The running program found that there was no log output in the console, but there was something in the hello2.log file, which was in line with our expectations:
And the log format matches that defined in the pattern= "% d {HH:mm:ss,SSS}% p% c (% L) -% m% n" format.
Further practice of Log4J
Pom.xml configuration:
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-logging Org.springframework.boot spring-boot-starter-log4j2
Log4j2.xml configuration
Springboot-web logs/$ {app_name}
Controller code:
Package com.hansonwang99.controller;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping ("/ testlogging") public class LoggingTestController {private final Logger logger = LogManager.getLogger (this.getClass ()); @ GetMapping ("/ hello") public String hello () {for (int item0)
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.