SpringBoot日志
1.日志框架
日志框架分为日志门面和日志实现
日志门面:slf4j
日志实现:log4j,log4j2,logback
SpringBoot底层为Spring框架,spring默认使用JCL日志门面,
而SpringBoot默认使用Slf4j,实现选用logback
2.slf4j的使用
1.如何在系统中使用slf4j
日志记录方法的调用,不应该针对实现层,而应该对接口也就是日志门面进行调用。
//摘自官方文档
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");
}
}
每一个日志的实现框架都要有自己的配置文件。使用slf4j之后,配置文件还是做成日志实现框架的配置文件
2.框架日志框架统一
SpringBoot下边的Spring等框架使用的日志框架不同,需进行统一
-
将系统中其他日志框架排除
-
用中间包替换原有日志框架
-
导入slf4j及其实现
3.SpringBoot日志结构
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.8.RELEASE</version>
springboot的日志功能
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
底层依赖关系
1) SpringBoot底层使用logback,slf4j进行日志记录
2) SpringBoot把其他日志替换成slf4j
3)中间替换包
4) 如果需要引入其他框架,需排除框架的默认日志
4. 使用示例
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggingApplicationTests {
//记录日志
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
//日志级别,由低到高
logger.trace("这是trace日志");
logger.debug("debug");
logger.info("info日志");
logger.warn("警告");
logger.error("出错了");
}
}
从控制台打印的日志来看,SpringBoot使用的日志级别是info,我们也可以进行修改,在配置文件中,我们可以对日志做很多配置
5. SpringBoot修改默认日志配置
#日志输入文件
logging.file=springbootdemo.log
#配置日志文件路径
logginf.path=
#配置日志级别
logging.level.root=warn
#定制日志格式
logging.pattern.console=
logging.pattern.file=
.............
6.SpringBoot指定日志配置
给类路径下放置每个日志框架自己的配置文件;SpringBoot便不使用自己的默认配置
[外链图片转存失败(img-rQIuSZsY-1568103155112)(D:\md_doc\images\1568041958099.png)]
不加-spring的xml文件,直接被日志框架识别
加上的文件,日志文件由SpringBoot加载
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
指定某段配置在该段执行
</springProfile>
<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
3.切换日志框架
配置logback-spring.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{YY-mm-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!--<logger name="com.wang123net.App" level="debug" />-->
<!--root设置全局的log level-->
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>
配置pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除默认日志配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<!-- 导入logback日志的jar -->
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
结果