springboot的日志组件

一、简述

  Springboot本身为slf4j、log4j(含log4j2)、logback提供了默认的配置文件;在默认的设置中,springboot是将日志信息仅打印在控制台,不输出到日志文件中的。

  在依赖spring-boot-starter-web中,包含了logback的依赖;也就是说,如果引用了<artifactId>spring-boot-starter-web</artifactId>,就使用了自带的logback;

  如果不想使用springboot带的logback,可以在引用依赖的时排除logback;

<dependencies>    
    <dependency>    
        <groupId>org.springframework.boot</groupId>    
        <artifactId>spring-boot-starter-web</artifactId>    
        <exclusions><!-- 去掉默认配置的logging -->    
            <exclusion>    
                <groupId>org.springframework.boot</groupId>    
                <artifactId>spring-boot-starter-logging</artifactId>    
            </exclusion>    
        </exclusions>    
    </dependency>    
    <dependency> <!-- 引入log4j2依赖或其它日志组件 -->    
        <groupId>org.springframework.boot</groupId>    
        <artifactId>spring-boot-starter-log4j2</artifactId>    
    </dependency>    
</dependencies>    

 

二、使用自定义的日志组件

  springboot支持使用自定义的日志组件和配置文件:排除对默认日志框架的依赖,引入所使用的日志组件的依赖;

  配置文件命名为log4j2-spring.xml 或 log4j2.xml (其它同理,如logback-spring.xml或logback.xml),且推荐使用 XXX-spring.xml的形式。这样无需在application.yml配置;

猜你喜欢

转载自www.cnblogs.com/chenhao0302/p/9184441.html
今日推荐