文章目录
- 前言
- 一、内置配置
- 1. 关联依赖
- 2. 内置配置
- 二、自定义配置
- 1. 日志级别
- 2. 彩色日志
- 3. 自定义配置
- 4. 增加打印语句
- 5. 效果展示
- 总结
前言
网关层作为我们程序的主入口,有着至关重要的作用,下面我们通过自定义Logback配置增强网关层的日志输出,为我们的网关提供更加详细和更加细化的日志输出。
大家有没有好奇过一件事,为什么我们的Spring Boot项目新建完,日志组件就能刷刷地打印呢?这是因为Spring Boot帮助我们集成了Logback并定义了默认的logger和appender,接下来我们来进行详细的解析。
一、内置配置
1. 关联依赖
spring-boot-starter-web>spring-boot-starter>spring-boot-starter-logging>logback-classic>logback-core
2. 内置配置
base.xml:定义了基本配置和引入其他配置文件
defaults.xml:定义了彩色日志,日志变量和日志logger
console-appender.xml:定义了控制台输出appender
file-appender.xml:定义了文件输出appender
二、自定义配置
我们自定义配置生效后,Spring Boot内置配置就不再生效,大家要知道这点
设计思路:
- debug.log(存储debug及高级别日志)
- info.log(存储info及高级别日志)
- error.log(存储error及高级别日志)
1. 日志级别
我们只关注三个常用的:DEBUG、INFO、ERROR即可
TRACE<DEBUG<INFO<WARN<ERROR<FATAL
2. 彩色日志
颜色
blue
cyan
green
magenta
red
yellow
样式
bold
(粗体)faint
(淡色)highlight
(高亮)
3. 自定义配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 引入Spring Boot的默认logback配置 -->
<property name="CONSOLE_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(%applicationName[%15.15t]){faint} %clr(${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}:%L) %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="FILE_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } %applicationName[%t] ${LOG_CORRELATION_PATTERN:-}%-40.40logger{39}:%L : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!-- 自定义控制台输出模板 -->
<property name="LOG_HOME" value="../logs/gateway/"/>
<property name="LOG_FILE" value="gateway"/>
<property name="LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START" value="false"/>
<property name="LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE" value="10MB"/>
<property name="LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP" value="0"/>
<property name="LOGBACK_ROLLINGPOLICY_MAX_HISTORY" value="7"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${CONSOLE_LOG_THRESHOLD}</level>
</filter>
<encoder>
<pattern>${CONSOLE_PATTERN}</pattern>
<charset>${CONSOLE_LOG_CHARSET}</charset>
</encoder>
</appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<pattern>${FILE_PATTERN}</pattern>
<charset>${FILE_LOG_CHARSET}</charset>
</encoder>
<file>${LOG_HOME}${LOG_FILE}_debug.log</file>
<!--大小和时间基于滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}${LOG_FILE}_debug.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
<maxFileSize>${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}</maxFileSize>
<totalSizeCap>${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}</totalSizeCap>
<maxHistory>${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-7}</maxHistory>
</rollingPolicy>
</appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>${FILE_PATTERN}</pattern>
<charset>${FILE_LOG_CHARSET}</charset>
</encoder>
<file>${LOG_HOME}${LOG_FILE}_info.log</file>
<!--大小和时间基于滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}${LOG_FILE}_info.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
<maxFileSize>${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}</maxFileSize>
<totalSizeCap>${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}</totalSizeCap>
<maxHistory>${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-7}</maxHistory>
</rollingPolicy>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>${FILE_PATTERN}</pattern>
<charset>${FILE_LOG_CHARSET}</charset>
</encoder>
<file>${LOG_HOME}${LOG_FILE}_error.log</file>
<!--大小和时间基于滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}${LOG_FILE}_error.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
<maxFileSize>${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}</maxFileSize>
<totalSizeCap>${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}</totalSizeCap>
<maxHistory>${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-7}</maxHistory>
</rollingPolicy>
</appender>
<!-- 设置特定包的日志级别 -->
<logger name="org.springframework" level="INFO"/>
<logger name="org.hibernate" level="INFO"/>
<logger name="com.alibaba" level="INFO"/>
<logger name="io.netty" level="INFO"/>
<logger name="reactor.netty" level="INFO"/>
<!-- Root Logger -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="DEBUG_FILE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
</configuration>
4. 增加打印语句
package org.example.gateway.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Create by zjg on 2024/8/3
*/
@Component
public class GatewayApplicationAware implements ApplicationContextAware {
Logger logger = LoggerFactory.getLogger(GatewayApplicationAware.class);
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
}
}
5. 效果展示
总结
回到顶部
有了详细的日志文件记录,我们就再也不怕程序报错了,加油,奥利给!