In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "detailed introduction of springboot log function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "springboot log function of the detailed introduction" bar!
one。 Mainstream log framework
There are many log frameworks on the market. JUL (java.util.logging), JCL (Apache Commons Logging), Log4j,Log4j2,Logback, SLF4j, jboss-logging, etc. Spring Boot uses JCL,spring-boot-starter-logging in the framework content department in the form of slf4j+logback, and Spring Boot can also automatically adapt (jul, log4j2, logback) and simplify configuration.
When writing about the two configuration modes of log4j (xml and propertiies), I was thinking, "Why do you need to import self4j, log4j, and integrate jar packages when using log4j?"
After some twists and turns, I finally got the answer. In fact, the answer is very simple. Logs are divided into two categories: log facade and log implementation. The relationship between these two is equivalent to the relationship between interfaces and implementation classes in java. Let's make a simple classification of the above logging framework:
Log facade log implementation JCL (Jakarta Common Logging) Log4jSLF4J (Simple Logging for Java) JUL (java.util. Logging) jboss-loggingLog4j2...Logback.
There is not an one-to-one correspondence between them, but they just write a beautiful point of view.
Second, the use of SpringBoot: the underlying is the Spring framework, and the Spring framework defaults to using JCLSpringBoot when SLF4j and logback are selected for future development, the call to the logging method should not directly call the implementation class of the log, but the method in the log abstraction layer; import the jar of slf4j and the implementation of logback into the system, jarpublic class HelloWorld {
Logger logger = LoggerFactory.getLogger (HelloWorld.class)
}
1. Introduction of jar package
As you can see from the above, importing logs requires selecting a log facade and a log implementation in the table above. Take Slf4j as an example to show the corresponding relationship as follows:
Here is another problem. Springboot uses SLF4j and logback by default, and the underlying spring uses JCL by default. Then if you import Hibernate (jboss-logging), how can you make other frameworks use slf4j for output together with me?
When viewing the official documentation of Slf4j, there is indeed a solution, as follows:
The solution is as follows:
Exclude other logging frameworks in the system first, replace the original logging framework with a tundish, and import slf4j other log relationships in springboot.
Org.springframework.boot
Spring-boot-starter
Image
Summary:
At the bottom of SpringBoot, slf4j+logback is also used for logging. SpringBoot also replaces other logs with slf4j; intermediate replacement packages such as log4j-to-slf4j.jar....
Org.springframework
Spring-core
Commons-logging
Commons-logging
Springboot is handled in the same way as the above illustration, first converting everything else to Slf4j, which supports a variety of logging implementations. Springboot2.1.6.RELEASE imports Slf4j, logback and log4j by default, so you don't need to import log-related jar packages to use log features!
2. The compilation of configuration files. Each log implementation framework has its own configuration file. After using slf4j, the configuration file is still made into the configuration document of the log implementation framework itself. For the configuration file of log4j, please refer to: for the configuration file of https://javacfox.github.io/2019/06/28/Log4j%E6%97%A5%E5%BF%97%E9%85%8D%E7%BD%AE/logback, please refer to: # specify the path of the log configuration file
# logging.config=classpath:/logback-spring.xml
# Log file storage path
# logging.path=/spring/log
# specify log file name
# logging.file=my.log
# maximum amount of history log files saved
# logging.file.max-history=100000
# specify the log printing level, and you can configure multiple logs
# logging.level.com.hiberate.orcal_and_hiberate=debug
# #
# maximum space for log files
# # logging.file.max-size=10M
# Log output format:
#% d indicates date and time
#% thread indicates thread name
#%-5level: the level displays 5 characters wide from the left
#% logger {50} indicates that the logger name is up to 50 characters long, otherwise it is divided by period.
#% msg: log messages
# n is a newline character
# Log console print format
# logging.pattern.console=%d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]%-5level%logger {50} -% msg%n
#
# # time format of log output
# logging.pattern.dateformat=%d {yyyy-MM-dd HH:mm:ss.SSS}
#
# # format of log output to file
# logging.pattern.file=%d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]%-5level%logger {50} -% msg%n
#
# # add an output level to the output log
# logging.pattern.level=%5p
#
# logging.exception-conversion-word=%wEx
Thank you for reading, the above is the content of "detailed introduction of springboot log function". After the study of this article, I believe you have a deeper understanding of the detailed introduction of springboot log function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.