SpringBoot系列_03_SpringBoot热部署和日志

热部署

步骤

①添加依赖

<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
JULslf4j
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值