Created
July 14, 2021 09:44
-
-
Save ololx/6ae29bc3cbaa30b93a0ccc245981ea48 to your computer and use it in GitHub Desktop.
This is a short code examples for realizing log tracing && log marking in Spring-Boot Applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import feign.RequestInterceptor; | |
import feign.RequestTemplate; | |
import lombok.extern.slf4j.Slf4j; | |
import org.slf4j.MDC; | |
import org.springframework.stereotype.Component; | |
/** | |
* @project some-api | |
* @created 2021-07-09 15:37 | |
* <p> | |
* @author Alexander A. Kropotin | |
*/ | |
@Slf4j | |
@Component("FeignLogFiltering") | |
public class FeignLogFiltering implements RequestInterceptor { | |
@Override | |
public void apply(RequestTemplate requestTemplate) { | |
String requestId = MDC.get("traceId"); | |
if(requestId != null) { | |
requestTemplate.header("traceId", requestId); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration > | |
<include resource="/org/springframework/boot/logging/logback/base.xml" /> | |
<variable name="log-file-path" value="./log" /> | |
<variable name="application-name" value="vtd-dc-api" /> | |
<variable name="log-server-address" value="172.17.0.1" /> | |
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> | |
<layout class="ch.qos.logback.classic.PatternLayout"> | |
<Pattern> | |
%green(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%-5level) %yellow([traceId=%X{traceId}, spanId=%X{spanId}]) %green(%logger{36}) : %msg%n%throwable | |
</Pattern> | |
</layout> | |
</appender> | |
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
<file>${log-file-path}/${application-name}.log</file> | |
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | |
<Pattern>%d %level %logger{36} [%mdc] : %msg%n%throwable</Pattern> | |
</encoder> | |
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
<fileNamePattern>${log-file-path}/archived/${application-name}-%d{yyyy-MM-dd}.%i.log | |
</fileNamePattern> | |
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |
<maxFileSize>2048MB</maxFileSize> | |
</timeBasedFileNamingAndTriggeringPolicy> | |
</rollingPolicy> | |
</appender> | |
<root level="INFO"> | |
<appender-ref ref="Console" /> | |
<appender-ref ref="RollingFile" /> | |
</root> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.extern.slf4j.Slf4j; | |
import org.slf4j.MDC; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.filter.OncePerRequestFilter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.UUID; | |
/** | |
* @project some-api | |
* @created 2021-07-09 15:37 | |
* <p> | |
* @author Alexander A. Kropotin | |
*/ | |
@Slf4j | |
@Component("LogFiltering") | |
public class LogFiltering extends OncePerRequestFilter { | |
private static final String REQUEST_ID = "traceId"; | |
@Override | |
protected void doFilterInternal(HttpServletRequest request, | |
HttpServletResponse response, | |
FilterChain filterChain) throws ServletException, IOException { | |
String requestId = request.getHeader(REQUEST_ID); | |
if (requestId == null) { | |
requestId = UUID.randomUUID().toString(); | |
} | |
MDC.put(REQUEST_ID, requestId); | |
try { | |
log.trace("Started process request with {} : {}", REQUEST_ID, requestId); | |
filterChain.doFilter(request, response); | |
} finally { | |
MDC.clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
logback example for Loki Appender