spring logback 简介
依赖图
可以发现logback在spring-boot-starter
依赖里面,所以引入了springboot都自带logback
graph LR
logback-->spring-boot-starter-logging-->spring-boot-starter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| +--- org.springframework.boot:spring-boot-starter -> 2.2.2.RELEASE | +--- org.springframework.boot:spring-boot:2.2.2.RELEASE | | +--- org.springframework:spring-core:5.2.2.RELEASE | | | \--- org.springframework:spring-jcl:5.2.2.RELEASE | | \--- org.springframework:spring-context:5.2.2.RELEASE | | +--- org.springframework:spring-aop:5.2.2.RELEASE | | | +--- org.springframework:spring-beans:5.2.2.RELEASE | | | | \--- org.springframework:spring-core:5.2.2.RELEASE (*) | | | \--- org.springframework:spring-core:5.2.2.RELEASE (*) | | +--- org.springframework:spring-beans:5.2.2.RELEASE (*) | | +--- org.springframework:spring-core:5.2.2.RELEASE (*) | | \--- org.springframework:spring-expression:5.2.2.RELEASE | | \--- org.springframework:spring-core:5.2.2.RELEASE (*) | +--- org.springframework.boot:spring-boot-autoconfigure:2.2.2.RELEASE | | \--- org.springframework.boot:spring-boot:2.2.2.RELEASE (*) | +--- org.springframework.boot:spring-boot-starter-logging:2.2.2.RELEASE | | +--- ch.qos.logback:logback-classic:1.2.3 | | | +--- ch.qos.logback:logback-core:1.2.3 | | | \--- org.slf4j:slf4j-api:1.7.25 -> 1.7.29 | | +--- org.apache.logging.log4j:log4j-to-slf4j:2.12.1 | | | +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.29 | | | \--- org.apache.logging.log4j:log4j-api:2.12.1 | | \--- org.slf4j:jul-to-slf4j:1.7.29 | | \--- org.slf4j:slf4j-api:1.7.29 | +--- jakarta.annotation:jakarta.annotation-api:1.3.5 | +--- org.springframework:spring-core:5.2.2.RELEASE (*) | \--- org.yaml:snakeyaml:1.25
|
logback-spring.xml
介绍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds" debug="false"> <springProperty scope="context" name="APP_NAME" source="spring.application.name" /> <springProperty scope="context" name="LOG_TYPE" source="spring.cloud.config.logback-profile" /> <property name="LOG_PATH" value="logs" /> <property value="${LOG_PATH}/${APP_NAME}.log" name="LOG_FILE_NAME" /> <property value="${LOG_PATH}/${APP_NAME}-%d{yyyy-MM-dd}-%i.log" name="LOG_FILE_NAME_PATTERN" /> <property name="LOG_FORMAT" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE_NAME}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE_NAME_PATTERN}</fileNamePattern> <MaxHistory>30</MaxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${LOG_FORMAT}</pattern> </layout> </appender>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${LOG_FORMAT}</pattern> </layout> </appender>
<logger name="com.exxk" level="debug" /> <logger name="org.springframework" level="debug" additivity="false"></logger>
<root level="info"> <appender-ref ref="stdout" /> <appender-ref ref="${LOG_TYPE}" /> </root> </configuration>
|
application.properties
1 2 3
| spring.application.name=testDemo
spring.cloud.config.logback-profile=FILE
|
log 日志脱敏和超长日志处理
需求:
因为日志里面含有大量的base64的图片数据,各处都有打印,导致日志过大,日志不美观排查问题不方便
参考
logback-spring.xml
一般人不敢动系列之—基于logback的日志“规范”和“脱敏”