四.Spring boot日誌介紹

Spring boot日誌介紹

1.1 經常使用的日誌框架分爲接口庫和實現庫

clipboard.png

1.2 spring 的日誌介紹

  • spring框架默認選擇的是JCL
  • spring boot框架默認選擇的是SLF4j + Logback

1.3 SLF4J的使用

案例:web

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

1.3.1與其餘實現庫整合圖(官網):

clipboard.png

1.3.2 如何解決多個框架默認日誌不統一的問題?

clipboard.png

  • 第一步:排除其餘日誌框架
  • 第二步:使用中間包替換原有日誌包
  • 第三步:導入slf4j實現包

1.4 spring boot + slf4j+Logback

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>

1.4.1 在哪排除其餘框架的默認日誌包

排除spring 使用的commons-loggingspring

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1.4.2 使用中間包替換原有日誌包

clipboard.png

1.4.3 spring boot日誌配置

  • spring boot默認級別爲info
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
private Logger logger = LoggerFactory.getLogger(ApplicationTests.class);
@Test
public void logTest() {
    logger.trace("--This is trace log");
    logger.debug("-- --This is debug log");
    logger.info("-- -- --This is info log");
    logger.warn("-- -- -- --This is warn log");
    logger.error("-- -- -- -- --This is error log");
    }
}

運行結果:
: -- -- --This is info log
: -- -- -- --This is warn log
: -- -- -- -- --This is error logapache

  • 簡單配置案例
#設置日誌有顏色的輸出
spring:
  output:
    ansi:
      enabled: always
#日誌配置
logging:
  #日誌級別設置
  level:
    #指定包生效
    com.lvmama: debug
  #日誌文件路徑(當前工程根目錄下spring.log)
  path: /log/springboot
  • spring boot 其餘日誌配置
logging.file
logging.file.max-size
logging.file.max-history
logging.path
logging.pattern.console
logging.pattern.dateformat
logging.pattern.file
logging.pattern.level
PID

1.4.4 自定義日誌配置

clipboard.png

When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.

在resource目錄下添加指定的log.xml,spring boot默認只用自定義的日誌配置,如logback在resource目錄下添加logback.xml或logback-spring.xml便可。不過官網推薦使用logback-spring的方式,由於能夠使用<springProfile>高級特性,動態使用日誌配置。springboot

Because the standard logback.xml configuration file is loaded too early, you cannot use extensions in it. You need to either use logback-spring.xmlor define a logging.config property.

測試:app

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <springProfile name="dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ---dev--- [%thread] - %-5level %logger{50} - %msg%n</pattern>
            </springProfile>
            <springProfile name="!dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] - %-5level %logger{50} - %msg%n</pattern>
            </springProfile>
        </layout>
    </appender>

mvn spring-boot:run框架

控制檯輸出:spring-boot

2018-05-20 17:55:44.708 - [main] - INFO  com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 4288 (D:\workspace\spring-boot\target\classes started by Administrator in D:\workspace\spring-boot)
2018-05-20 17:55:44.732 - [main] - DEBUG com.lvmama.SpringBootInstaller - Running with Spring Boot v1.5.14.BUILD-SNAPSHOT, Spring v4.3.17.RELEASE
2018-05-20 17:55:44.733 - [main] - INFO  com.lvmama.SpringBootInstaller - No active profile set, falling back to default profiles: default
2018-05-20 17:55:45.224 - [background-preinit] - INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final
2018-05-20 17:55:48.158 - [main] - INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2018-05-20 17:55:48.236 - [main] - INFO  org.apache.catalina.core.StandardService - Starting service [Tomcat]

mvn spring-boot:run -Drun.profiles=dev //指定dev環境 ​測試

2018-05-20 17:59:25.500 ---dev--- [main] - INFO  com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 2880 (D:\workspace\spring-boot\target\classes started by Administrator in D:\workspace\spring-boot)
2018-05-20 17:59:25.505 ---dev--- [main] - DEBUG com.lvmama.SpringBootInstaller - Running with Spring Boot v1.5.14.BUILD-SNAPSHOT, Spring v4.3.17.RELEASE
2018-05-20 17:59:25.506 ---dev--- [main] - INFO  com.lvmama.SpringBootInstaller - The following profiles are active: dev
2018-05-20 17:59:26.007 ---dev--- [background-preinit] - INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final
2018-05-20 17:59:27.749 ---dev--- [main] - INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2018-05-20 17:59:27.769 ---dev--- [main] - INFO  org.apache.catalina.core.StandardService - Starting service [Tomcat]
相關文章
相關標籤/搜索