logback 配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<springProperty scope="context" name="LOG_HOME" source="logging.file.path" defaultValue="/var/logs/test-server"/>
<springProperty scope="context" name="LOG_NAME" source="spring.application.name" defaultValue="test-device"/>
<property name="CONSOLE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%5level) %magenta(${PID}) --- [%15.15thread] %cyan(%-40.40logger) : %msg%n"/>
<property name="FILE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5level ${PID} --- [%15.15thread] %-40.40logger : %msg%n"/>
<property name="STDOUT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - [%tid] - %msg%n"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<!--保证控制台打印链路信息-->
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${STDOUT_PATTERN}</pattern>
</layout>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>MARKER_MP_NOTIFY</marker>
</evaluator>
<onMismatch>NEUTRAL</onMismatch>
<onMatch>DENY</onMatch>
</filter>
<file>${LOG_HOME}/${LOG_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${LOG_NAME}_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<!-- 单个日志文件最多50MB -->
<maxFileSize>50MB</maxFileSize>
<!-- 日志文件保留天数 -->
<maxHistory>30</maxHistory>
<!-- 最大不能超过20GB, 到了这个值, 就会删除旧的日志 -->
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<!--保证日志文件打印链路信息-->
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${STDOUT_PATTERN}</pattern>
</layout>
</encoder>
</appender>
<!--整合skyWalking,日志上传至skyWalking-->
<property name="SKY_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss} | %-5level | %thread | %tid | %logger{50} %L\ | %msg%n" />
<appender name="SKY_LOG" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender" >
<!-- 对日志进行格式化 -->
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${SKY_LOG_PATTERN}</pattern>
</layout>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="SKY_LOG"/>
</root>
</configuration>
主要将此处的layout 改成自定义的
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${SKY_LOG_PATTERN}</pattern>
</layout>
#替换成自定义的
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.test.test.common.core.logback.TraceIdPatternLogbackLayout">
<pattern>${SKY_LOG_PATTERN}</pattern>
</layout>
</encoder>
LogbackPatternConverter
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
/**
* ClassName LogbackPatternConverter
*
* @author wang
* Date 2022/6/27 14:29
*/
public class LogbackPatternConverter extends ClassicConverter {
public LogbackPatternConverter() {
}
@Override
public String convert(ILoggingEvent iLoggingEvent) {
return "TID: 20220627";
}
}
标题 TraceIdPatternLogbackLayout
import ch.qos.logback.classic.PatternLayout;
import org.springframework.stereotype.Component;
/**
* ClassName TraceIdPatternLogbackLayout
*
* @author wang
* Date 2022/6/27 14:28
*/
@Component
public class TraceIdPatternLogbackLayout extends PatternLayout {
public TraceIdPatternLogbackLayout() {
}
static {
DEFAULT_CONVERTER_MAP.put("tid", LogbackPatternConverter.class.getName());
}
}
参考博客
https://blog.csdn.net/a17816876003/article/details/115682360