一.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.日志参数配置
logging.file 和logging.path
logging.file: # 日志文件,绝对路径或相对路径
logging.path: # 保存日志文件目录路径
logging.config: # 日志配置文件,Spring Boot默认使用classpath路径下的日志配置文件,如:logback.xml
logging.level.xxx: # 日志级别