热部署
步骤
①添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
②配置idea
Ctrl+Shift+Alt+/,选择Register
日志
概述
发展史
程序员A发明了开发了log4j,然后开源了log4j
程序员A进入了apache
JDK开发了JUL(java.util.logging)
市面上出现了很多的日志框架,Apache开发了日志门面JCL(不实现日志功能,只整合日志)
程序员A离开了apache,独自开发了日志门面slf4j
- 适配器用来连接其他日志门面
- 桥接器用来连接其他日志框架
apache重新开发了log4j2
程序员A重新开发了logback
主要日志框架
日志实现 | 日志门面 |
---|---|
log4j(已被淘汰) | JCL |
JUL | slf4j |
log4j2 | |
logback |
日志桥接关系图
SpringBoot默认日志框架
SpringBoot使用了slf4j+logback组合来作为默认日志框架
注意点
- JCL动态查找机制进行日志实例化,执行顺序为commons-logging.properties -> 系统环境变量 -> log4j -> JUL -> simplelog -> nooplog
日志配置
SpringBoot日志全局配置
logging.level.root
指定根目录下所有日志的日志级别
logging.level.[具体类路径]
指定具体类路径下的日志级别
logging.pattern.console
文件中使用的日志模式,也就是系统变量FILE_LOG_PATTERN的值
logging.pattern.dateformat
配置日志日期格式,也就是系统变量LOG_DATEFORMAT_PATTERN的值
logging.file.name
设置文件的名称,如果没有设置路径默认会在项目的相对路径下,如果需要指定路径可以使用 路径+文件名 的方式
logging.file.path
指定日志文件路径,默认名称为spring.log
logging.file.max-size
最大日志文件大小
logging.file.total-size-cap
要保留的日志备份的总大小
logging.file.max-history
要保留的最大归档日志文件数
logging.file.clean-history-on-start
是否在启动时清除存档日志文件
自定义日志配置文件
可以通过在类路径中包含日志配置文件来激活各种日志记录系统,各种日志系统对应的默认配置文件名如下图所示:
注意:如果结合SpringBoot提供Profile或者springProperty来控制日志生效时,需要将logback.xml改成logback-spring.xml,因为logback.xml会在被SpringBoot容器加载前先被logback加载到,此时logback无法解析springProfile 将会报错
<configuration>
<jmxConfigurator />
<!-- 可以引用SpringBoot全局配置文件中的配置项 -->
<springProperty scope="context" name="dateformat" source="logging.pattern.dateformat"
defaultValue="-yyyy-MM-dd"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<springProfile name="dev">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date [%thread] %-5level %logger{25} ---- %msg%n</Pattern>
</layout>
</springProfile>
<springProfile name="staging">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date [%thread] %-5level %logger{25} ++++ %msg%n</Pattern>
</layout>
</springProfile>
</appender>
<root level="debug">
<appender-ref ref="console" />
</root>
</configuration>
切换日志框架
切换Log4j2日志框架步骤:
第一步:由于spring-boot-starter默认依赖了logback的桥接器logback-classic,而slf4j只能接受一种桥接器,因此需要先排除logback-classic
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
第二步:添加log4j2的启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐log4j2</artifactId>
</dependency>
第三步:添加log4j2的日志配置文件,因为SpringBoot全局配置文件中的日志配置大部分是针对logback的,log4j2等其他日志框架可能无法解析
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>