logback日志格式模板,基于TraceId搜索完整的请求链路日志
日志打印格式:(可以基于TraceId:4d484c2a110eae9d来搜索完整的请求链路日志 2023-08-28 15:06:53.635 [http-nio-8081-exec-37][myProject,4d484c2a110eae9d,fad0ea6a223ebe5c,false] ERROR c.a.o.c.base.MyExceptionAdvisor#handleException [line:31] - MyExceptionAdvisor.handleExceptionjava.lang.RuntimeException: the value of code :null not supported,please check
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="LOG_HOME" value="/usr/log/myProject" />
<property name="FILE_LOG_NAME" value="myProject" />
<property name="application_name" value="myProject"/>
<property name="FILE_LOG_NAME_ERR" value="myProjectErr" />
<!-- root日志级别-->
<property name="LOG_LEVEL" value="INFO"/>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread][${application_name},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] %-5level %class{36}#%M [line:%L] - %msg%xEx%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${LOG_LEVEL}</level>
</filter>
</appender>
<appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME}/${FILE_LOG_NAME}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/${FILE_LOG_NAME}-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
<MaxHistory>100</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>200MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread][${application_name},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] %-5level %class{36}#%M [line:%L] - %msg%xEx%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${LOG_LEVEL}</level>
</filter>
</appender>
<appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME}/${FILE_LOG_NAME_ERR}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/${FILE_LOG_NAME_ERR}-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
<MaxHistory>100</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>200MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread][${application_name},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] %-5level %class{36}#%M [line:%L] - %msg%xEx%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<root level="INFO">
<appender-ref ref="fileLog" />
<appender-ref ref="consoleLog"/>
<appender-ref ref="errorLog"/>
</root>