一、配置
Spring Boot默认使用LogBack日志系统,如果不需要更改为其他日志系统如Log4j等,则无需多余的配置,LogBack默认将日志打印到控制台上。
1、如果要使用LogBack,原则上是需要添加dependency依赖的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
但是因为新建的Spring Boot项目一般都会引用spring-boot-starter
或者spring-boot-starter-web
,而这两个起步依赖中都已经包含了对于spring-boot-starter-logging
的依赖,
所以,无需额外添加依赖,配置logback-spring.xml就可以了。以logback-spring.xml命名,spring会自动识别加载。
2、如果需要切换Log4j2,那么在pom.xml中需要排除springboot自带的commons‐logging,然后再引入log4j2的依赖:
<!--排除 commons‐logging-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>commons‐logging</groupId>
<artifactId>commons‐logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
再引入log4j.properties文件就可以了。
二、日志级别
由低到高: trace < debug < info < warn < error ,设置的级别越低显示的日志级别的信息越多。默认是info级别。
三、日志的组成部分
输出的节点(items)如下:
1. 日期和时间 - 精确到毫秒,且易于排序。
2. 日志级别 - ERROR, WARN, INFO, DEBUG 或 TRACE。
3. Process ID。
4. 一个用于区分实际日志信息开头的---分隔符。
5. 线程名 - 包括在方括号中(控制台输出可能会被截断)。
6. 日志名 - 通常是源class的类名(缩写)。
7. 日志信息。
四、日志配置
通过将适当的库添加到classpath,可以激活各种日志系统。然后在classpath的根目录(root)或通过Spring Environment的 logging.config 属性指定的位置提供一个合适的配置文件来达到进一步的定制。
1、在application.properties文件中添加日志文件路径
2、配置logback-hnsw.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 -->
<!-- scan:当此属性设置为true时,配置文档如果发生改变,将会被重新加载,默认值为true -->
<!-- scanPeriod:设置监测配置文档是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。
当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
<!-- debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="30 seconds" debug="false">
<property name="log_pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%logger] [%line] - %m %n"></property>
<property name="file_charset" value="UTF-8"></property>
<property name="max_file_size" value="100MB"></property>
<property name="max_history" value="10"></property>
<property name="file_prefix" value="logs/hnsw"></property>
<!-- 输出到控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log_pattern}</pattern>
<charset>${file_charset}</charset>
</encoder>
</appender>
<!-- 输出到文档 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文档的路径及文档名 -->
<File>${file_prefix}.log</File>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${file_prefix}.%d.%i.log</fileNamePattern>
<!--日志文档保留天数-->
<maxHistory>${max_history}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${max_file_size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>${log_pattern}</pattern>
<charset>${file_charset}</charset>
</encoder>
<!-- 此日志文档只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<!-- level为 ERROR 日志,时间滚动输出 -->
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${file_prefix}-error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${file_prefix}-error.%d.%i.log</fileNamePattern>
<maxHistory>${max_history}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${max_file_size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>${log_pattern}</pattern>
<charset>${file_charset}</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<!-- 生产环境 -->
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.×××">
<level value="INFO"></level>
<appender-ref ref="FILE" />
<appender-ref ref="FILE_ERROR" />
</logger>
</configuration>