spring boot日志框架

一.日志框架:

  市场上存在非常多的日志框架。JUL ( java.util.logging ) , JCL ( ApacheCommons Logging) , Log4j , Log4j2 , Logback. SLF4j、jboss-logging等。Spring Boot在框架内容部使用JCL , spring-boot-starter-logging采用了slf4j+ logback的形式, Spring Boot也能自动适配( jul、log4j2、 logback )并简化配置
 

二.各种日志框架向slf4j转换

如何让系统中所有的日志都统一到slf4j ;
1、将系统中其他日志框架先排除出去;
2、用中间包来替换原有的日志框架;
3、我们导入slf4j其他的实现

三.jar包依赖树

四.总结:


1 )、SpringBoot底层也是使用slf4j+logback的方式迸行日志记录

2)、SpringBoot也把其他的日志都替换成了slf4j ;

3)、中囘替换包

4)、如果我们要引入其他框架?一定要把这个框架的默认日志依赖移除掉

五.实例:

1.jar包

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>

2.java测试代码


import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


import com.example.demo.model.Student;
import com.example.demo.service.studentService;



@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

	org.slf4j.Logger logger=LoggerFactory.getLogger(getClass());
	@Test
    public void loadTest() {
		//日志级别由高到低
    	logger.trace("跟踪日志");
    	logger.debug("debug日志");
    	logger.warn("警告日志");
    	logger.error("错误日志");
    }

}

猜你喜欢

转载自blog.csdn.net/yiye2017zhangmu/article/details/82500853