SpringBoot教程(八)springboot日誌配置
阿新 • • 發佈:2018-12-26
一.springboot預設日誌配置
1.springboot預設日誌配置是使用logback
2.引用springboot依賴包時會自動載入依賴jar包
3.專案中配置logback
===========引用springboot依賴的pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spf.demo</groupId> <artifactId>spf-demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> </dependencies> </project>
======application.properties檔案
###Tomcat Config server.port=8080 server.tomcat.uri-encoding=UTF-8 ####DataSource Config spring.datasource.platform=mysql spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lpinfo?useSSL=false&useUnicode=true&characterEncoding=utf8 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 ###SPBdefaultLoggin Config logging.level.root=info
package com.spf.demo; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * Unit test for simple App. */ @RunWith(SpringRunner.class) @SpringBootTest(classes = AppTest.class) public class AppTest { Logger logger = LoggerFactory.getLogger(getClass()); // 日誌輸出等級 高>低 error > warn > info > debug >trace //SpringBoot預設是info級別,只會輸出info及以上級別的日誌 @Test public void test() { logger.error("如果配置檔案日誌等級error--->[日誌只能輸出error等級日誌]"); logger.warn("如果配置檔案日誌等級warn--->[日誌只能輸出error,warn等級日誌]"); logger.info("如果配置檔案日誌等級info--->[日誌只能輸出error,warn,info等級日誌]"); logger.debug("如果配置檔案日誌等級debug--->[日誌只能輸出error,warn,info,debug等級日誌]"); logger.trace("如果配置檔案日誌等級trace--->[日誌只能輸出error,warn,info,debug,trace等級日誌]"); } }
執行結果:
二.springboot預設日誌logback的詳細配置
1.日誌引數配置
application.properties中如果配置 logging.file 和logging.path
logging.file: # 日誌檔案,絕對路徑或相對路徑
logging.path: # 儲存日誌檔案目錄路徑
logging.config: # 日誌配置檔案,Spring Boot預設使用classpath路徑下的日誌配置檔案,如:logback.xml
logging.level.xxx: # 日誌級別