In order to facilitate the call of the log in the future, you should not directly call the log implementation class, but call the method in the log abstraction layer
Import the sl4j and logback jar packages into the system: log can be used after importing the following jar packages:
package com.example.log.demo.hello;
// Import log related test package
import org.slf4j.LoggerFactory;
public static void main(String [] args)
//The test uses log . I tried it. At present , the debug content of this logger only works in the normal class. The call of the startup class ( DemoApplication ) and the test class ( DemoApplicationTest ) does not display the log ?
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.debug( "yanruTODO test log 1" ) ;
The following is the detailed log frame structure:
Light blue is the adaptation interface layer, and dark blue is the log implementation layer.
Each log implementation framework has its own configuration file (such as log4j). After using slf4j, the configuration file is still made into the log implementation framework's own configuration file;
When the A system (slf4j+logback) was developed, various log records were used, and based on various other frameworks;
Unified logging, even if other frameworks use slf4j for output together with me, some intermediate conversion packages-adapters are used to achieve.
How to unify all logs in the system to slf4j:
1. First exclude other frameworks in the system;
2. Replace the original log framework with an intermediate package;
3. We import other implementations of slf4j;
Right-click in the pom.xml of the project: Diagrams->Show Dependencies
(1) The bottom layer of Spring Boot also uses sl4j+logback for logging
(2) Spring Boot also converts other types of logs to sl4j
(3) Use intermediate conversion package
The following is the intermediate conversion package:
(4) If we want to introduce other frameworks, we must remove the default log dependency of this framework!
The Spring framework uses comments-logging, and the SpringBoot configuration file removes the dependency of commons-logging by default. The <exclusion> keyword, my new project guide configuration is not like this anymore. There must be doubts here (2019-1-1)
Spring Boot configures the log for us by default
Directly in the test class, you can use Logger to print logs:
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
// Import log related packages
public class DemoApplicationTests {
Logger logger = LoggerFactory.getLogger(getClass());
//The following log levels, from low to high: trace<debug<info<warn<error
// logs can be controlled to print only a certain level and higher-level log , the Spring the Boot default to give us info level log
Logger a .trace ( " This is a trace log " ) ;
logger .debug( " This is a debug log " ) ;
logger .info( " This is an info log " ) ;
logger .warn( " This is a warn log " ) ;
logger .error( " This is the error log " ) ;
Modify the log printing level in the configuration file.properties file
# Specify this com.example inside the package uses trace levels of log
logging.level.com.example=trace
The result is run, it will print all levels of log
Spring Boot modifies the default configuration of the log in .properties:
# Specify this com.example inside the package uses trace levels of log
logging.level.com.example=trace
# Specify a log file is generated in the current project, the default path is not specified, it indicates when the project, such as = springboot.log
#logging.file=D:/springboot.log
# In the root path of the current disk project is located, create spring folder and inside the log file folder, use spring.log as the default file
# In the console log crude output format ( log format string prefix such as date or something ), the following configuration will not take effect, specifically go online to check! , And the default configuration of logback used by my project by default does not have the opportunity to edit the log format without this configuration
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%\n
#Specify the format of the log output to the file , the following configuration will not take effect, please check online for details! , And the default configuration of logback used by my project by default does not have the opportunity to edit the log format without this configuration
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} === %msg%\n
To see what configuration items in the log default configuration allow editing, check it in the springboot package:
Just put each logging framework's own configuration under the classpath, and Spring Boot does not apply the default configuration file.
If it is logbackk, you can put logback.xml or logback-spring.xml, but Spring recommends you to put logback-spring.xml, the difference is:
logback.xml: directly recognized by the log system
logback-spring.xml : A certain configuration will only take effect in a certain development or production environment, you can use the profile feature of SpringBoot, otherwise, the configuration items in the xml may report errors (for example, if you want to specify the production environment and test separately Each environment has a different log output format)
The following is specific, how to write the configuration file name when various logs want to write custom configuration:
Determine the tags of the current profile environment in logback-spring.xml:
//The current profile mode is in the dev development environment, this configuration condition takes effect
It is possible to switch logs, but I am not going to learn or take notes anymore. I cannot see the special significance of this. .
Alt+Insert brings up a pop-up window for adding get and set methods, and methods can be added in batches;
Some common package descriptions about Maven packages:
<!--Spring module for unit testing -->
<groupId>org.springframework.boot</groupId>